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. :/

Name: Anonymous 2009-11-25 1:25

int nums[1000];
...
while(... && !nums[i])
 ...
 nums[i]++

GUARANTEED O(1) COMPLEXITY

Name: Anonymous 2009-11-25 1:27

>>9

haha, cheers pal. But I don't know what a hashtable is, not got that far and the mark scene is pretty specific. I'll have a google.

Name: Anonymous 2009-11-25 1:32

scheme*

Name: Anonymous 2009-11-25 1:38

import java.util.Random;
import java.text.DecimalFormat;
public class Random100 {
    public static void main(String[] args) {
        boolean[] random = new boolean[1000];
        Random rng = new Random();
        int n;
        for(int i = 0; i < 100; i++) {
            while(random[(n = rng.nextInt(1000))]);
            random[n] = true;
            System.out.print(new DecimalFormat("000").format(n)+((i%10==9)?"\n":" "));
        }
    }
}

Name: Anonymous 2009-11-25 2:42

import java.io.*;

public class ImportantProgram
{
    public static void main (String[] args) throws IOException
    {
        System.out.println("O(1) my anus");
    }
}

Name: Anonymous 2009-11-25 2:52

>>10
I surely hope you meant O(n) by that. Or you were talking about the best case scenario.

Name: Anonymous 2009-11-25 11:25



    import java.util.Random;
    import java.util.*;
    public class Random100 {
        public static void main(String[] args) {
            ArrayList<Integer> res;
            Random rnd = new Random();
            int n = 0;
            while(res.size()<100) {
                if(!res.contains(n = rnd.nextInt(1000)))
                    res.add(n);
            }
        }
    }

Name: HMA FAN 2009-11-25 12:00

Generating your ints with random() is kind of nasty. Your algorithm isn't guaranteed to terminate. Here's something more predictable.


import java.text.DecimalFormat;
import java.util.Collections;
import java.util.ArrayList;

public class Main {
    final static int maxn = 1000;
    final static int width = 10;
    final static int height = 10;
   
    public static void main(String[] args) {
        ArrayList numbers = new ArrayList();
        for( int i = 0; i < maxn; ++i )
            numbers.add(i);
        Collections.shuffle( numbers );

        DecimalFormat fmt = new DecimalFormat(" 000 ");
        for( int i = 0; i < height; ++i ) {
            for( int j = 0; j < width; ++j ) {
                System.out.print( fmt.format( numbers.get(i*width + j) ) );
            }
            System.out.println();
        }
    }
}

Name: HMA FAN 2009-11-25 12:18

And for the entertainment, a hash set. By the way, >>9-kun:
use Random to produce an int, wrap it in an Integer object, and check whether than Integer object currently exists in the Hash

It's a set. You don't need to check if it's already in there. Just keep adding until you have enough.


import java.text.DecimalFormat;
import java.util.Iterator;
import java.util.Random;
import java.util.LinkedHashSet;

public class Main {
    final static int maxn = 1000;
    final static int width = 10;
    final static int height = 10;
   
    public static void main(String[] args) {
        Random rnd = new Random();
        try{
            LinkedHashSet numbers = new LinkedHashSet();
            while( numbers.size() < width * height )
                numbers.add( rnd.nextInt(maxn) );

            DecimalFormat fmt = new DecimalFormat(" 000 ");
            Iterator it = numbers.iterator();
            for( int i = 0; i < height; ++i ) {
                for( int j = 0; j < width; ++j ) {
                    System.out.print( fmt.format( (Integer)it.next() ) );
                }
                System.out.println();
            }
        } catch( Exception e ) {
            System.out.println( e.toString() );
        }
    }
}

Name: Anonymous 2009-11-25 12:29

>>18
Using a DecimalFormat to print regular numbers? That's ENTERPRISE-quality work you've got there.

Name: Anonymous 2009-11-25 12:42

>>19
Ripped off >>13 because I couldn't be assed to research Java I/O. Should be System.out.format(" %3d ", n ) I guess. IHBT. IHAND.

Name: Anonymous 2009-11-25 13:15

>>17
Generating your ints with random() is kind of nasty. Your algorithm isn't guaranteed to terminate.
Anyone who needed this explanation needs to stop programming immediately, and go read their SICP.

Name: Anonymous 2009-11-25 13:24

>>21
I refuse to read my SICP.

Name: Anonymous 2009-11-25 13:59

>>22

●█████▄▄▄▄▄▄▄▄
▄▅███████▅▄▃▂
███LISP Tank███████►
◥☼▲⊙▲⊙▲⊙▲⊙▲⊙▲☼◤

Name: Anonymous 2009-11-25 14:12

length (nub l) == length l

Name: Anonymous 2009-11-25 14:23

>>24
let l = "your gay"

Name: Anonymous 2009-11-25 14:31

>>25
That better be in some do-syntax boy or else your going to get a synt*** Stack Overflow

Name: Anonymous 2009-11-25 21:35

>>25,26
What about my *** stack overflow?

Name: Anonymous 2009-11-26 3:52

OVERFLOW MY AN[m]*** stack overflow

Name: Anonymous 2009-11-26 3:52

OVERFLOW MY AN*** stack overflow

Name: Anonymous 2009-11-26 6:55

Fucking candlejack threads should go bac*** Exception: stack overf

Name: Anonymous 2009-11-26 7:20

import Data.List (nub)

hasDupes :: [a] -> Bool
hasDupes xs = xs /= nub xs

Of course this only works in boost.

Name: Anonymous 2009-11-26 9:32

>>31
Boost::Hell::TemplateRing::NotLambda::List

Name: ​​​​​​​​​​ 2010-10-24 17:18

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