mardi 24 février 2015

List of Non-Repeating Ints in Java? Assignment

I'm trying to create a list of 20 integers between 0 and 26 (so in the 1-25 range) that does not repeat as a part of an assignment. I thought I had it figured out, but the program keeps looping over and over without ever ending. Can anyone help me out?



import java.util.Random;

public class prog433a
{
public static void main(String args[])
{
Random r = new Random();

int[] list = new int[20];

for (int k = 0; k < list.length; k++)
{
boolean notADupe = false;

while (notADupe == false)
{
list[k] = r.nextInt(25) + 1;
for (int j = 0; j < list.length; j++)
{
if (list[j] == list [k] && j != k)
{
notADupe = true;
}
else
{
notADupe = false;
break;
}

}

System.out.println(list[k]);
}
}
}
}


EDIT: This is different from the other question because I am trying to figure out how to check for uniqueness using the methods that I am allowed to use in my assignment (essentially, the ones I'm already using in the code).





Aucun commentaire:

Enregistrer un commentaire