package cn.itcast.test;
/*
* 获取10个1-20之间的随机数,要求不能重复
* 随机数 new Random().nextInt(20)+1
* 获取到的随机数,存储到集合
*/
import java.util.*;
public class ListTest {
public static void main(String[] args) {
Random r = new Random();
List<Integer> list = new ArrayList<Integer>();
//进入死循环
while(true){
//获取随机数
int num = r.nextInt(20)+1;
//判断随机数在不在集合中
if(!list.contains(num)){
//随机数存储到集合
list.add(num);
if(list.size()==10){
break;
}
}
}
for(Integer i : list){
System.out.println(i);
}
}
}
|
|