1介绍
java.utils.Collections 是集合⼯具类,⽤来对集合进⾏操作。
2常用API
public static <T> boolean addAll(Collection<T> c, T... elements):往集合中添加⼀些元素。
public static void shuffle(List<?> list) :打乱顺序,打乱集合顺序。
public static <T> void sort(List<T> list):将集合中元素按照默认规则排序。
3使用
public class Demo01 {
public static void main(String[] args) {
ArrayList<String> arrayList = new ArrayList<>();
Collections.addAll(arrayList,"C","B","E","A","D");
System.out.println("元素加入集合:"+arrayList);
Collections.shuffle(arrayList);
System.out.println("打乱集合中元素顺序:"+arrayList);
Collections.shuffle(arrayList);
System.out.println("打乱集合中元素顺序:"+arrayList);
Collections.sort(arrayList);
System.out.println("排序结果如下:"+arrayList);
}
} |
|