import java.util.ArrayList;
import java.util.Collections;
public class Demo111 {
public static void main(String[] args) {
//编写一个程序,获取10个1至20的随机数,要求随机数不能重复。
ArrayList<Integer> al = new ArrayList<>();
while (true) {
//将随机数转化为int类型
int temp = (int)(Math.random()*20 + 1);
//当集合不包含这个随机数就添加
if (!al.contains(temp)) {
al.add(temp);
}
//当集合长度为10就跳出循环
if (al.size() == 10) {
break;
}
}
//排序
Collections.sort(al);
System.out.println(al);
}
} |