java随机数1―10不重复,输出的值顺序要打乱

来源:百度知道 编辑:UC知道 时间:2024/05/30 13:55:26
每次输出的顺序都不一样!谁知道

public static void main(String[] args) {
boolean[] bol = new boolean[11];
for(int i=1;i<11;i++)
bol[i] = false;
bol[0]=true;
for (int i = 0; i < 10; ) {
double a = Math.random() * 10;
a = Math.ceil(a);
int randomNum = new Double(a).intValue()%11;
if(bol[randomNum])
continue;
bol[randomNum]= true;
i++;
System.out.print(randomNum+" ");
}

}

public static void main(String[] args) {
Object[] objs = new Object[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
List list = new ArrayList(Arrays.asList(objs));
for (int i = 10; i > 0; i--) {
int index = new Random().nextInt(i);
System.out.print(list.get(index) + " ");
list.remove(index);
}
}