Collections工具类
- 概念:集合工具类,定义了除了存取以外的集合常用方法。
1. 常用方法:
public static void reverse(List<?> list)
//反转集合中元素的顺序
public static void shuffle(List<?> list)
//随机重置集合元素的顺序
public static void sort(List<T> list)
//升序排序(元素类型必须实现Comparable接口)
2. 演示
List<Integer> list = new ArrayList<Integer>();
list.add(20);
list.add(10);
list.add(30);
list.add(90);
list.add(70);
System.out.println(list.toString());
Collections.sort(list);
System.out.println(list.toString());
int i = Collections.binarySearch(list, 10);
System.out.println(i);
List<Integer> list2 = new ArrayList<Integer>();
for (int i1 = 0; i1 < 5; ++i1) {
list2.add(0);
}
Collections.copy(list2, list);
System.out.println(list2.toString());
Collections.reverse(list2);
System.out.println(list2.toString());
Collections.shuffle(list2);
System.out.println(list2.toString());
3. 补充
Integer[] arr = list.toArray(new Integer[0]);
System.out.println(arr.length);
String[] nameStrings = {"tang", "he", "yu"};
List<String> list3 = Arrays.asList(nameStrings);
System.out.println(list3);