Collections使用

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]
******************************************************************************/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值