首页 > 其他 > 详细

【ArrayList:获取 10 个 1-20 之间的随机数,要求不能重复】

时间:2019-01-31 12:29:44      阅读:164      评论:0      收藏:0      [点我收藏+]
package com.companyname.common.test;

import java.util.ArrayList;
import java.util.Random;

/**
 * @Description 获取 10 个 1-20 之间的随机数,要求不能重复
 * @Author Created by shusheng.
 * @Email shusheng@yiji.com
 * @Date 2018/12/14
 */
public class RandomDemo {

    public static void main(String[] args) {

        Random random = new Random();

        ArrayList<Integer> arrayList = new ArrayList<Integer>();

        int x=0;

        while(x<10){
            int y = random.nextInt(20);
            if(!arrayList.contains(y)){
                arrayList.add(y);
                x++;
            }
        }

        for(Integer i:arrayList){
            System.out.println(i);
        }

    }
}

 

【ArrayList:获取 10 个 1-20 之间的随机数,要求不能重复】

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

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