Collections集合概述和使用演示示例

05d13dcbd17fb45754ba029ca0a4d6a6.png

public class CollectionsDemo {
    public static void main(String[] args) {
        //创建集合对象
        List<Integer> list = new ArrayList<Integer>();

        //添加元素
        list.add(50);
        list.add(20);
        list.add(30);
        list.add(10);
        list.add(40);

        //输出集合
        System.out.println(list);
    }
}

输出结果:

//public static<T extends Comparable<? super T>> void sort(List<T> list):将指定的列表按升序排序

Collections.sort(list);

运行结果:

//public static void reverse(List <?> list):反转指定列表中元素的顺序

        Collections.reverse(list);

运行结果:

//public static void shuffle(List<?> list):使用默认的随机源随机排列指定的列表

Collections.shuffle(list);

运行结果:

第一次:

 第二次:

 第三次:

猜你喜欢

转载自www.cnblogs.com/pxy-1999/p/12690149.html
今日推荐