首页 > 其他 > 详细

【HashSet:获取10个1至20的随机数,要求随机数不能重复】

时间:2019-01-31 11:05:57      阅读:233      评论:0      收藏:0      [点我收藏+]
package com.yjf.esupplier.common.test;

import java.util.HashSet;
import java.util.Random;

/**
 * @author shusheng
 * @description 获取10个1至20的随机数,要求随机数不能重复
 * @Email shusheng@yiji.com
 * @date 2018/12/17 15:33
 */
public class HashSetDemo {

    public static void main(String[] args) {
        // 创建随机数对象
        Random r = new Random();

        // 创建一个Set集合
        HashSet<Integer> ts = new HashSet<Integer>();

        // 判断集合的长度是不是小于10
        while (ts.size() < 10) {
            int num = r.nextInt(20) + 1;
            ts.add(num);
        }

        // 遍历Set集合
        for (Integer i : ts) {
            System.out.println(i);
        }
    }

}

 

【HashSet:获取10个1至20的随机数,要求随机数不能重复】

原文:https://www.cnblogs.com/zuixinxian/p/10340911.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!