java.util.Collections提供了一些有关List操作的静态方法,很有效的对List进行操作
1.sort(List)---对List中的元素排序
2. shuffle(List)----对List中的元素随机排序
3.reverse(List)---对List中的元素反转
。。。。。。。。
import java.util.*;
public class TestList {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
List<String> ls=new LinkedList<String>();
for(int i=0;i<=9;i++){
ls.add("a"+i);
}
System.out.println(ls);
//随机排列
Collections.shuffle(ls);
System.out.println(ls);
//逆序排列
Collections.reverse(ls);
System.out.println(ls);
//排序
Collections.sort(ls);
System.out.println(ls);
//二分查找
System.out.println(Collections.binarySearch(ls, "a2"));
//定义一个新的List
List<String> lst=new LinkedList<String>();
for(int j=0;j<20;j++){
lst.add("b"+j);
}
System.out.println(lst);
Collections.copy(lst, ls);
System.out.println(lst);
}
}
//结果(不唯一)为:
/*
[a0, a1, a2, a3, a4, a5, a6, a7, a8, a9]
[a4, a3, a6, a8, a1, a9, a7, a0, a2, a5]
[a5, a2, a0, a7, a9, a1, a8, a6, a3, a4]
[a0, a1, a2, a3, a4, a5, a6, a7, a8, a9]
2
[b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15, b16, b17, b18, b19]
[a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, b10, b11, b12, b13, b14, b15, b16, b17, b18, b19]
*/