1

I'm looking to create a dynamic amount of arrays of random integers and then put them into an array list. Later I want to use each of these arrays separately to test for quick sort functionality. I'm having problems with adding the object List[] into the ArrayList.

//Create dynamic amount of random arrays
public static ArrayList<int[]> Randomizer(int arrays, int size, int seed){
    ArrayList<int[]> Tests = new ArrayList<int[]>(arrays);


    int[] List = new int[size];
    for (int j = 0; j < arrays; j++){
        Random r = new Random(seed+j);  
        for(int i = 0; i < size; i++){
            List[i] = r.nextInt(5*size);//Multiplier for how big the numbers get
            System.out.print(List[i] + ",");
        }
        System.out.println();
        Tests.add(j, List);
    }


    return Tests;
}


public static void main(String[] args) {
int tests = 5;
int size = 4;
ArrayList<int[]> Test = Randomizer(tests,size,10); //1st = Number of Tests
                                                   //2nd = Number of Digits
                                                   //3rd = seed for Randomizer
for(int i = 0; i < Test.size(); i++){
System.out.println(Test.get(i));
}
}

}

  • I just threw your code into Eclipse and everything seems to be working fine - are you confused by the output when you `println(Test.get(i))`? The result you are getting is the memory location for the arrays. To see the numbers from your main method you'll need to do something different, but the results you are getting are the arrays. – Nate Anderson Nov 03 '16 at 17:44
  • I guess my question should change then to how would I use one array from the database of arrays inside Test now. For example, printing only one. Then I can use a for loop to run through them all. – Marcin Wisniowski Nov 03 '16 at 17:48
  • 1
    Depends on what you want to do with those arrays. For printing the array look at: [this](http://stackoverflow.com/questions/409784/whats-the-simplest-way-to-print-a-java-array). Keeping them in a list will let you pass all of them to a method or constructor, then it's just a matter of using Test.get(i) to grab the one you want. – Nate Anderson Nov 03 '16 at 17:51
  • 1
    @MarcinWisniowski you have to be more clear buddy in explaining what exactly you need – Shreyas Sarvothama Nov 03 '16 at 17:53
  • As I wrote above, I'm going to be using the arrays for testing quicksort functionalities to overall mass check them and find the fastest/etc. – Marcin Wisniowski Nov 03 '16 at 17:53
  • @NateAnderson Thanks for the article, I've fixed my problem. – Marcin Wisniowski Nov 03 '16 at 17:58
  • @NateAnderson I'm having issues where my output is the last List[] that was imported for all of the array values. – Marcin Wisniowski Nov 03 '16 at 18:24
  • You were able to get all the `int[]` into the List, so you should be able to get them back out if you use the correct method. – Nate Anderson Nov 03 '16 at 19:25

1 Answers1

1

The problem with your code was that you were storing the same array 5 times into the ArrayList, so when printing during generation it printed correct numbers, but later you couldn't get them out. Each iteration of the for loop was overwriting the values generated earlier.

Here is the corrected code:

private static ArrayList<int[]> randomizer(int arrays, int size, int seed){
    ArrayList<int[]> tests = new ArrayList<>(arrays);
    for (int j = 0; j < arrays; j++) {
        int[] list = new int[size];
        Random r = new Random(seed + j);
        for(int i = 0; i < size; i++) {
            list[i] = r.nextInt(5 * size); // Multiplier for how big the numbers get
        }
        tests.add(j, list);
    }
    return tests;
}

public static void main(String[] args) {
    int tests = 5;
    int size = 4;
    ArrayList<int[]> arrays = randomizer(tests, size, 10);
    for (int i = 0; i < arrays.size(); i++){
        int[] ints = arrays.get(i);
        for (int j = 0; j < ints.length; j++) {
            System.out.print(ints[j] + ",");
        }
        System.out.println();
    }
}

Basically you needed to move the int[] list = new int[size]; line inside the for loop, so that you are actually creating new arrays instead of using the same one each time.

You can now replace the printing loop in the main() method with whatever you like, like your quick sort tests. Let me know if anything still doesn't work.

Nohus
  • 739
  • 8
  • 16