public static <T> void show(List<T> list){
for (T t : list) {
System.out.print(t+" ");
}
}
public static void main(String[] args) {
ArrayList<Integer> arr = new ArrayList<>();
arr.add(6);
arr.add(1);
arr.add(2);
arr.add(7);
//sort排序(升序)
Collections.sort(arr);
show(arr);
System.out.println();
//降序1(使用重写比较器)
Collections.sort(arr, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
if(o1==o2) return 0;
return o1>o2?-1:1;
}
});
show(arr);
System.out.println();
//降序2(使用反转升序)
Collections.sort(arr);
Collections.reverse(arr);
show(arr);
System.out.println();
//打乱
Collections.shuffle(arr);
show(arr);
System.out.println();
//二分查找(必须是有序的不然输出-4)
Collections.sort(arr);
System.out.println(Collections.binarySearch(arr, 6));
//list转为数组
Integer[] array = arr.toArray(new Integer[5]);
for (Integer integer : array) {
System.out.print(integer+" ");
}
System.out.println();
//数组转list
List<Integer> integers = Arrays.asList(array);
show(integers);
}