public static void main(String[] args) {
int arr[] = {1,3,5,4,6,2};
ArrayList<Integer> arrInt = new ArrayList<Integer>();
for(int i=0; i<arr.length;++i){
arrInt.add(arr[i]);
}
//sort操作
System.out.println("before sort:"+arrInt);
Collections.sort(arrInt);
System.out.println("after sort:"+arrInt);
//Shuffling操作
System.out.println("before shuffle:"+arrInt);
Collections.shuffle(arrInt);
System.out.println("after shuffle:"+arrInt);
//Reverse操作
System.out.println("before Reverse:"+arrInt);
Collections.reverse(arrInt);
System.out.println("after Reverse"+arrInt);
//Copy操作
ArrayList<Integer> arrInt1 = new ArrayList<Integer>();
int arr1[] = {0,0,0,0,0,0,0};
for(int i=0; i<arr1.length;++i){
arrInt1.add(arr1[i]);
}
System.out.println("before copy:"+arrInt1);
Collections.copy(arrInt1, arrInt);
System.out.println("after copy:"+arrInt1);
//min操作
System.out.println("min:"+Collections.min(arrInt));
//max操作
System.out.println("max:"+Collections.max(arrInt));
//lastIndexOfSubList
ArrayList<Integer> arrInt2 = new ArrayList<Integer>();
System.out.println("lastIndexOfSubList:"+Collections.lastIndexOfSubList(arrInt, arrInt2));
//IndexOfSubList
ArrayList<Integer> arrInt3 = new ArrayList<Integer>();
System.out.println("IndexOfSubList:"+Collections.indexOfSubList(arrInt, arrInt3));
//Rotate
System.out.println("before rotate:"+arrInt);
Collections.rotate(arrInt, 2);
System.out.println("after rotate:"+arrInt);
//binarySearch
//replaceAll
System.out.println("before replaceAll:"+arrInt);
Collections.replaceAll(arrInt, 3, 9);
System.out.println("after replaceAll:"+arrInt);
//synchronizedXXX()
Collections.synchronizedCollection(arrInt);
//emptyXXX
Collections.emptyList();
//singletonXXX()
System.out.println("Collections.singleton(1)"+Collections.singleton("1"));
//unmodificableXXX()
System.out.println("Collections.unmodifiableList(arrInt)"+Collections.unmodifiableList(arrInt));
//frequency
System.out.println("Collections.frequency(arrInt, 5)"+Collections.frequency(arrInt, 5));
//Fill操作
System.out.println("before Fill:"+arrInt);
Collections.fill(arrInt, 0);
System.out.println("after Fill:"+arrInt);
}
/***************************打印结果******************************************
before sort:[1, 3, 5, 4, 6, 2]
after sort:[1, 2, 3, 4, 5, 6]
before shuffle:[1, 2, 3, 4, 5, 6]
after shuffle:[1, 6, 5, 4, 3, 2]
before Reverse:[1, 6, 5, 4, 3, 2]
after Reverse[2, 3, 4, 5, 6, 1]
before copy:[0, 0, 0, 0, 0, 0, 0]
after copy:[2, 3, 4, 5, 6, 1, 0]
min:1
max:1
lastIndexOfSubList:6
IndexOfSubList:0
before rotate:[2, 3, 4, 5, 6, 1]
after rotate:[6, 1, 2, 3, 4, 5]
before replaceAll:[6, 1, 2, 3, 4, 5]
after replaceAll:[6, 1, 2, 9, 4, 5]
Collections.singleton(1)[1]
Collections.unmodifiableList(arrInt)[6, 1, 2, 9, 4, 5]
Collections.frequency(arrInt, 5)1
before Fill:[6, 1, 2, 9, 4, 5]
after Fill:[0, 0, 0, 0, 0, 0]
******************************************************************************/
Collections使用
最新推荐文章于 2020-01-07 11:29:37 发布