Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Java Help :O

Name: ipoopmypants !49.zlWoq9M 2009-11-24 22:53

Anyone know how in Java I check check an array to see if there are any duplicates?

        int[] random = new int[100];
        Random randomGenerator = new Random();
        boolean inArray = true;

for(int i = 0; i < random.length; i++)
        {
            random[i] = randomGenerator.nextInt(999);
            if(random[i] == /*CHECK ALL OTHER INDEXES IN THE ARRAY*/)
            {
                random[i]++;
            }
           
            }

Name: Anonymous 2009-11-25 1:19

>>8
OH.  Now I see what you're really trying to do.
Yeah, the Hashtable method >>6 recommended would probably work easiest.

-Build a Hashtable suitable for 100 capacity (probably the worst part of the whole program).

-Looping: use Random to produce an int, wrap it in an Integer object, and check whether than Integer object currently exists in the Hash.  If no, then add it and count 1; if not, then generate another int and try again.  Since hashes require two values, a key and a value, pass the key as the int/Integer generated and the value as its generation position (0-99).

-After you've counted 100, create the int[100] array.  Get the keys from the Hashtable, Iterate through them to get the values, then add the number in each key to the specific index of the array indicated by the value.
For example, for "Hashtable.add(RandomInt, Count)" insert as "Array[Count] = RandomInt"

Look at me.  I'm doing your homework. :/

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List