代码:
public class Test {
public static void main(String[] args) {
// 测试数据
int[] arr = {1, 4, 5, 6, 7, 3};
// 把数组中的数据添加到集合中
List<Integer> list = new ArrayList<>();
for (int i = 0; i < arr.length; i++) {
list.add(arr[i]);
}
// 一次次的取出集合中的数据,直到取完,不能使用for循环,毕竟list.size()在变化
while (list.size() > 0) {
// 取得集合中的任意下标
int i = (int) (Math.random() * list.size());
// 取出集合中的相关元素
System.out.println(list.get(i));
// 删除集合中的相关元素
list.remove(i);
}
}
}
结果:
5
7
6
4
1
3