package com.hm.cn;
import java.util.ArrayList;
import java.util.Collections;
public class CollectionsTools {
/**
* @Description:collections工具类
*/
public static void main(String[] args) {
ArrayList list = new ArrayList();
Collections.addAll(list,11,5,23,9,19);//给list赋值
System.out.println("list原来的顺序:"+list);
Collections.reverse(list);
System.out.println("list反转之后的顺序:"+list);
Collections.sort(list);
System.out.println("list排序之后的顺序"+list);
Collections.swap(list,2,3);
System.out.println("将下标2和下标3的数据位置互换"+list);
Collections.shuffle(list);//将list顺序打乱(洗牌)
System.out.println("shuffle洗牌之后的效果:"+list);
System.out.println("集合中最大的数为:"+Collections.max(list));
System.out.println("集合中最小的数为:"+Collections.min(list));
Collections.sort(list);
System.out.println("二分查找集合中5的位置:前提是有序的"+Collections.binarySearch(list,11));
}
}
|
|