因此,我尝试生成一个充满唯一随机整数的数组,我发现使用数组列表执行此操作将是最有效的方法。
public static int [] randArr(int n, int max){
randArr = new int[n];
Random rn = new Random();
Set<Integer> test = new HashSet<Integer>();
while(test.size() < n){
test.add(rn.nextInt(0, max));
}
}
Run Code Online (Sandbox Code Playgroud)
现在我尝试使用,randArr = test.toArray()但我不太确定括号里应该放什么,也不确定这是否真的可行。是否还有其他转换方法,因为我也不能简单地通过test.get(i)for 循环将 test 的整数分配给 randArr 。