我想生成此范围内 (0-255) 的 255 个唯一随机数。这样数组就不会包含重复的记录
short [] array =new short[255];
Random rand = new Random();
boolean flag=false;
for (int i=0;i<array.length;i++){
int random_integer = rand.nextInt(255-0) + 0;
for (int j=0;j<i;j++){
if ((short)random_integer==array[j]){
flag=true;
}
}
if (flag==false){
array[i]=(short)random_integer;
}
}
for (int i=0;i<array.length;i++){
System.out.println(array[i]);
}
Run Code Online (Sandbox Code Playgroud)
但我只得到前 20 0r 30 个具有值的项目,其余的数组项目等于零。