A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

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);
    }
}

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马