使用ArrayList获取10个1-20之间的随机数,要求不能重复
思路:
new集合new随机数判断是否重复一直到取到10个不同随机数为止。
public class Demo{
public static void main(String
[] args
){
ArrayList
<Integer> list
=new ArrayList<Integer>();
Random random
=new Random();
int num
;
int count
;
while(true){
num
=random
.nextInt(20);
if(!list
.contains(num
)){
list
.add(num
);
count
++;
if(count
==10){
break;
}
}
}
for(Integer i
:list
){
System
.out
.println(i
);
}
}
}