Java中对List集合内的元素进行顺序、倒序、随机排序的示例代码
1 import java.util.Collections; 2 import java.util.LinkedList; 3 import java.util.List; 4 5 public class Test { 6 7 List list = new LinkedList(); 8 public static void main(String[] args) { 9 List list = new LinkedList(); 10 for ( int i = 0 ; i < 9 ; i ++ ) { 11 list.add( " a " + i); 12 } 13 Collections.sort(list); // 顺序排列 14 System.out.println(list); 15 16 Collections.shuffle(list); // 混乱的意思 17 System.out.println(list); 18 19 Collections.reverse(list); // 倒序排列 20 System.out.println(list); 21 22 System.out.println(Collections.binarySearch(list, " a5 " )); // 折半查找 23 } 24 25 }