public class QuestionFour { /**
* @param args
*/
//定义一个长度为10的静态数组;
public static int[] temp = new int[10];
public static void main(String[] args) {
//定义一个RANDOM对象
Random rand = new Random();
//外层循环为数组赋随机值
for(int i = 0 ; i < temp.length ; i++){
//产生一个10的随机数
int number = rand.nextInt(10);
//为数组赋值
temp[i] = number;
//循环数组的每个元素
for(int j = 1 ; j<=i ; j++){
//从数组的第一个元素开始,循环到数组长度为J的时候,如果有重复的值,则重新获得随机数,并把随机数赋值给temp数组
if(temp[i] == temp[j]){
number = rand.nextInt(10);
temp[j] = number;
//如果没有重复的数,则打印
}else{
System.out.print(temp[i]);
break;
}
}
}
}
}
* @param args
*/
//定义一个长度为10的静态数组;
public static int[] temp = new int[10];
public static void main(String[] args) {
//定义一个RANDOM对象
Random rand = new Random();
//外层循环为数组赋随机值
for(int i = 0 ; i < temp.length ; i++){
//产生一个10的随机数
int number = rand.nextInt(10);
//为数组赋值
temp[i] = number;
//循环数组的每个元素
for(int j = 1 ; j<=i ; j++){
//从数组的第一个元素开始,循环到数组长度为J的时候,如果有重复的值,则重新获得随机数,并把随机数赋值给temp数组
if(temp[i] == temp[j]){
number = rand.nextInt(10);
temp[j] = number;
//如果没有重复的数,则打印
}else{
System.out.print(temp[i]);
break;
}
}
}
}
}
number = rand.nextInt(10);
temp[j] = number;
这段程序有Bug。不过更大的问题是这个算法思路很不好,效率很低。应该设计成不需要进行重复判断的,也就是保证取数据时绝对不会发生重复。
说实话,没具体看,有很多代码少 效率又高的方法
学java是面向对象不是面向过程,不必这么繁琐
Set<Integer> set=new HashSet<Integer>();
Random r=new Random();
while(true){
set.add(r.nextInt(10));
if(set.size()==10){
break;
}
}
你的思路是每次产生新的数之后遍历数组然后改正已有的与新数重复的数,那么你改正后的数只能保证和新产生的数无重复,但反而有可能和之前产生的数重复,所以算法并没能实际解决你的问题。
最自然的思路是每产生一个数就先判断数组中有没有这个数,如果没有再赋值到数组。
if(temp[i] == temp[j]){
number = rand.nextInt(10);
temp[j] = number;
改为
if(temp[i] == temp[j]){
number = rand.nextInt(10);
temp[i]=number;
当然,我更愿意相信是你手误……
final int len = 10;
Random rand = new Random();
int[] array = new int[len];
for(int i=0;i<len;i++){//遍历数组,填写随机数。
int r = rand.nextInt(len);
boolean old = false;//声明是否重复的标示符,重复时值为真。
do{//循环检查新生成的随机数,直到它真的不是重复的为止
old = false;//假设填写的随机数不是重复的
for(j=0;j<i;j++){//遍历以前生成的数字,看有没有与当前重复的
if(array[j]==r){
old=true;//设置当前数,是重复的。
r = rand.nextInt(len);//重新获取新随机数。
break;
}
}
}while(old);
array[i] = r;//为数组添加新元素
}
1.添加一个元素到数组中
2.确保新元素是不重复的
3.查找重复元素楼主算法,我没仔细看,有那时间,自己写一个了。
估计楼主算法的bug在于没有我那个do循环。
Random ran=new Random();
int[] num=new int[number];
for(int i=0;i<number;i++){
num[i]=ran.nextInt(number)+1;
for(int j=0;j<i;j++){
if(num[j]==num[i]){
num[i]=ran.nextInt(number)+1;
j=-1;
}
}
}
return num;
}
import java.util.HashSet;
import java.util.Iterator;
import java.util.Random;
import java.util.Set;
public class test2 {
public static void main(String[] args) {
Set<Integer> set=new HashSet<Integer>();
Random r=new Random();
while(true){
set.add(r.nextInt(100));
if(set.size()==10){
break;
}
}
Iterator<Integer> it=set.iterator();
while(it.hasNext()){
System.out.print(it.next());
System.out.print(" ");
}
}
}
懂点脑筋行吗?他只从0 - 10随机。
然后想产生10个不一样的数,不是0-9.能是啥?
你把这个改成
set.add(r.nextInt(100));
就不是0-9了。
嗯,虽没细看是否完全正确,但这个思路肯定是正确的。更加think in java。
用洗牌的方法即可
1.顺序产生0-9
2.遍历数组,每次随机产生一个下标(0-9),与当前位置的元素交换时间复杂度O(n)
import java.util.Arrays;
import java.util.Random;public class Test {
public static void main(String[] args){
System.out.println(Arrays.toString(getNums(10)));
}
static int[] getNums(int n){
int[] arrays = new int[n];
//顺序赋值
for(int i = 0;i < arrays.length;i++){
arrays[i] = i;
}
//洗牌
Random ran = new Random();
for(int i = 0;i < arrays.length;i++){
int index = ran.nextInt(n);//随机生成一个下标
//交换
int temp = arrays[index];
arrays[index] = arrays[i];
arrays[i] = temp;
}
return arrays;
}
}