ArrayList<integer> list = new ArrayList<integer>();
int x;
for (int i = 0; list.size() < 10; i++) {
x = 1 + (int)(Math.random()*10);//限定x值的范围 1-10
if (list.indexOf(x) == -1) {//判断x是否存在于list中 ! list.contains(x)
list.add(x);//不存在,则添加入list中
}
}
Collections.sort(list);//给list排序
System.out.print(Arrays.toString(list.toArray()));//数组打印
结果:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
浙公网安备 33010602011771号