集合--list和set的区别(以随机产生10个整数为例)

本文通过在[10,20)区间随机生成10个整数,对比分析Java中List和Set的区别。List允许元素重复,添加时需检查是否存在,而Set不允许重复,可直接通过size检查是否满足条件。同时提及使用数组生成整数的方法。" 102998577,9150254,NFS服务器搭建与autofs自动挂载教程,"['NFS', '文件系统', '服务器配置', '客户端', 'autofs']
摘要由CSDN通过智能技术生成

 以[10,20)区间随机产生10个整数为例展示:


    //list :有序  可重复
    public static void fun1(){
        List<Integer> list = new ArrayList<Integer>();
        do{
            int a = (int) (Math.random()*10+10);
            if(!list.contains(a)){
                list.add(a);
            }
        }while(list.size() <10);
        System.out.println(list);

    }
 //set无序 不可重复
    public static void fun3(){
        Set<Integer> set1 = new HashSet<>();
        do{
            int a = (int) (Math.random()*10+10);
            set1.add(a);

        }while(set1.size()<10);

        System.out.println(set1);
    }

:list可重复,所以数据

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值