import java.util.ArrayList;
import java.util.Random;
public class Test_4 {
public static void main(String[] args) {
Random r = new Random(); //Random r
ArrayList<Integer> coll = new ArrayList<>(); //ArrayList
int count = 0;
while (count < 10) { //输出10个数
int num = r.nextInt(20) + 1;
if (!coll.contains(num)) { //如果集合中不包含这个数就添加
coll.add(num);
count++; //计数
}
}
//遍历输出coll内的数
for (Integer x : coll) {
System.out.println(x);
}
}
}
ArrayList生成10个1到20之间的不重复的随机数
最新推荐文章于 2022-08-05 17:22:23 发布