题目描述:
请写程序将 1到10000之间的数打印输出。要求:
a)随机乱序
b)不能重复
c)全部输出,不能遗漏
d)如果要用到数据容器,只能用数组。
编码实现(main方法):
public static void main(String[] args) {
int first_array[]=new int[10000]; //生成一个10000长度的数组
for(int i = 0; i < 10000; i++){
first_array[i]=i+1; //赋值
}
Random r = new Random();
int index, tmp, rest = 10000;
for(int i = 0; i < 10000; i++){
index=r.nextInt(10000); //10000以内的随机数
//冒泡算法随机交换位置
tmp = first_array[index];
first_array[index] = first_array[rest - 1];
first_array[rest - 1] = tmp;
rest--;
}
for(int i = 0; i < 10000; i++){
System.out.println(first_array[i]); //输出结果
}
}