效果如图:
代码如下public class ArrayListToAry {
public static void main(String[] args) {
// 数组
int[] ary = new int[10];
// 集合 ,临时集合temp存放1~10个数字
ArrayList temp = new ArrayList();
//list集合存放需要的数字
ArrayList list = new ArrayList();
// 给集合添加1~10的数字
for (int i = 1; i
temp.add(i);
}
// while循环 随即抽取集合的数字给数组
int index = 0;
while (true) {
if(list.size()==10){
break;
}
int it = temp.get((int) (Math.random() * 10));
//如果list存在这个数组就继续循环
if (list.contains(it)) {
continue;
} else {
//如果list不存在这个数字,那么就把这个数字给数组,并且在list中加入这个数字
ary[index] = it;
list.add(it);
index++;
}
}
// /输出数组的元素
System.out.print("数组的元素:");
for (int i = 0; i
System.out.print(ary[i] + " ");
}
}
}