[英]hashset to remove duplicates from array Java
我目前正在尝试使我的Java程序不打印出重复的法律。 该软件是随机选择的,可打印1至3条法律,但我不希望重复。 我对Java还是很陌生,到目前为止只写了一些程序。 这是我到目前为止编写的最复杂的代码。
现在,我希望该软件在找到重复项之前再次运行随机文件,直到打印件中没有重复项为止。 反正有这样做吗?
/**
*Seeds is a random number to pick which if statement will be used.
* telling the software how many laws to use during its random
* generation process
*
*/
Random seeds = new Random();
seed = seeds.nextInt(3) + 1;
if (seed == 1){
/**Beginning of random law selection for universe. Must find way
*Must find way to get rid of duplicate answers
*/
final String[] Laws = {"Standard Physics", "Magic", "Mad Science",
"Psionics", "Substandard Physics","Exotic"};
Random random = new Random();
int index = random.nextInt(Laws.length);
System.out.println("Law:" + Laws[index]);
}
else if (seed == 2) {
final String[] Laws = {"Standard Physics", "Magic", "Mad Science",
"Psionics", "Substandard Physics","Exotic"};
Random random = new Random();
int index = random.nextInt(Laws.length);
int index2 = random.nextInt(Laws.length);
System.out.println("Law:" + Laws[index] + "," + Laws[index2]);
}
else {
final String[] Laws = {"Standard Physics", "Magic", "Mad Science",
"Psionics", "Substandard Physics","Exotic"};
Random random = new Random();
int index = random.nextInt(Laws.length);
int index2 = random.nextInt(Laws.length);
int index3 = random.nextInt(Laws.length);
System.out.println("Law: " + Laws[index] + "," + Laws[index2] +
"," + Laws[index3]);
}
解决这个问题的另一种方法是: 您想以随机顺序选择值。 这样,除非一个元素出现多次,否则您将不会获得重复。
List<String> laws = Arrays.asList("Standard Physics", "Magic", "Mad Science",
"Psionics", "Substandard Physics","Exotic");
Collections.shuffle(laws);
// select as many as you need
List<String> lawsSelected = laws.subList(0, 3);
这是一种方法。 您有3个if条件,因此需要3个布尔变量并将每个if关联一个,并在执行if时将其设为true。
Random seeds = new Random();
seed = seeds.nextInt(3) + 1;
boolean selected[] = new boolean[3];
selected[0] = selected[1] = selected[2] = false;//initially none of the if is executed
if (seed == 1 && !selected[seed-1]){//checking if not previously selected and "seed-1" since array indexes are from 0 to n-1
/**Beginning of random law selection for universe. Must find way
*Must find way to get rid of duplicate answers
*/
final String[] Laws = {"Standard Physics", "Magic", "Mad Science",
"Psionics", "Substandard Physics","Exotic"};
Random random = new Random();
int index = random.nextInt(Laws.length);
System.out.println("Law:" + Laws[index]);
selected[seed-1] = true;
}
else if (seed == 2 && !selected[seed-1]) {
final String[] Laws = {"Standard Physics", "Magic", "Mad Science",
"Psionics", "Substandard Physics","Exotic"};
Random random = new Random();
int index = random.nextInt(Laws.length);
int index2 = random.nextInt(Laws.length);
System.out.println("Law:" + Laws[index] + "," + Laws[index2]);
selected[seed-1] = true;
}
else if (!selected[seed-1]){
final String[] Laws = {"Standard Physics", "Magic", "Mad Science",
"Psionics", "Substandard Physics","Exotic"};
Random random = new Random();
int index = random.nextInt(Laws.length);
int index2 = random.nextInt(Laws.length);
int index3 = random.nextInt(Laws.length);
System.out.println("Law: " + Laws[index] + "," + Laws[index2] +
"," + Laws[index3]);
selected[seed-1] = true;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.