2

So basically I need to make a hash table, I understand how to do it with a string array but I need to do so with a random array I make.

Basically here is my random number code that another user gave me

    int[] array = new int[8000];
    Random rng = new Random();
    for (int i = 0; i < 8000; i++) {
    array[i] = rng.nextInt(65536);  
    }

I need this to somehow turn into something like this in my professor's example code

String[] array = { "100", "510", "170", "214", "268", "398",
            "235", "802", "900", "723", "699", "1", "16", "999", "890",
            "725", "998", "978", "988", "990", "989", "984", "320", "321",
            "400", "415", "450", "50", "660", "624" };

Which i tried and did this:

 int[] array = new int[8000];
 Random rng = new Random();
 for (int i = 0; i < 8000; i++) {
     array[i] = rng.nextInt(65536);
     String strI = Integer.toString(i);
     String[] array = {i + "," };

}

2
  • I need to make it a string variable in order to pass it into my method I have created for hash table function that takes a String array and string array length Commented Nov 8, 2015 at 4:01
  • 1
    edit your original post. Commented Nov 8, 2015 at 4:03

2 Answers 2

1

You can make it a String[] and fill that (and I would prefer using the array.length, instead of hardcoding 8000 again). Something like,

String[] array = new String[8000];
Random rng = new Random();
for (int i = 0; i < array.length; i++) {
     array[i] = String.valueOf(rng.nextInt(65536));
}
Sign up to request clarification or add additional context in comments.

Comments

0

The shortest way of writing this is to use a stream.

Random rng = new Random();
String[] array = rng.ints(8000, 0, 65536).mapToObj(Integer::toString).toArray(String[]::new);

However streams were only introduced in Java 8.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.