Collections工具类
- 概念:集合工具类,定义了除了存取以外的集合常用方法。
- 方法:
public static void reverse(List<?> list )
//反转集合中元素的顺序public static void shuffle(List<?> list )
//随机重制集合元素的顺序public static void sort(List<T> list )
//升序排序(元素类型必须实现Comparable接口)
public class Demo4 {
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
list.add(20);
list.add(5);
list.add(12);
list.add(30);
System.out.println("排序之前:" + list.toString());
Collections.sort(list);
System.out.println("排序之后:" + list.toString());
int i = Collections.binarySearch(list,12);
System.out.println(i);
List<Integer> dest = new ArrayList<>();
for (int k = 0; k < list.size(); k++) {
dest.add(0);
}
Collections.copy(dest,list);
System.out.println(dest.toString());
Collections.reverse(list);
System.out.println("反转之后:" + list);
Collections.shuffle(list);
System.out.println("打乱之后:" + list);
Integer[] arr = list.toArray(new Integer[10]);
System.out.println(arr.length);
System.out.println(Arrays.toString(arr));
String[] names = {"张三","里斯","王五"};
List<String> list1 = Arrays.asList(names);
System.out.println(list1);
Integer[] nums = {100,200,300};
List<Integer> list2 = Arrays.asList(nums);
System.out.println(list2);
}
}