Dax Duisado
Dax Duisado

Reputation: 197

Auto - generating arrays within a loop

EDIT: it is necessary for this to be a series of 1d Arrays. 2d Arrays / Arraylists can be used but the end result has to be in a series of 1d Arrays. Putting this into a 2d structure will just leave me with the same issue. (converting every row of the 2d array/list into a seperate array that is originally named (ideally consecutive integers after some string, such as Arr0, Arr1....as shown in the example).

I am looking for code for autogenerating arrays (of the same type; int[])

The code below hopefully shows what I am trying to accomplish:

for(int i=0; i<1000; i++) {
    int[] Arr"i" = new int[(200)];
    for(int j=0; j<200; j++) {
        Arr"i"[j] = j;
    }
}

So in the end I would have 1000 arrays named Arr0, Arr1, Arr2.....Arr999 and each of them would be filled with values of 0-199 in the respective index.

(they will all have different data, this is just a simple example to display my problem).

Is there any way to do this? I am still fairly new to java and it seems any possible solution to do this is a tad out of my league so to speak.

Thanks for any assistance

Upvotes: 2

Views: 3054

Answers (5)

pseudoramble
pseudoramble

Reputation: 2561

In Java, there is no way to create new variables like this at run time. They have to be named up front during compile time. So, you cannot quite take the approach you were starting with. That being said, you certainly have the right idea - Creating an array that consists of arrays in each slot.

As others have said, what you are looking for is a 2-dimensional array. Phrasing this another way, you're looking for an array of array's. You can imagine a 1D array as a set of individual slots, each able to store a single value within it. A 2D array is where each individual slot contains another array within it.

Accessing each array is as simple as iterating through each value in the array, and then iterating through each value of that sub array. In other words:

  1. Allocate a new array in the ith slot (with the appropriate size for that array)
  2. Create a for loop (from 0 -> size of the new array)
  3. Add new values to the ith by jth value (Arr[i][k] = x)

A more detailed explanation can be found here which provides a very useful image to depict what an array of arrays looks like, as well as some code examples.

Reading your edit, it sounds like you want these to be named as separate 1 dimensional arrays. While it's not possible to name new variables at run time like that, you could view each array within the 2D array as an individual row.

My suggestion is to see if you can refactor your code to have a method that will accept a 1D array and do whatever processing you need.

First, the method to process the 1D array:

/**
 * Prints each value x^2 in the given row
 */
private static void processRow(int[] row) {
    for (int x : row) {
        System.out.println("x^2 = " + (x*x));
    }
}

Once you have this in place, you can process an individual row in your 2D array like so:

public static void main(String[] args) {
    int[][] table = new int[10][10];

    // Allocate table with real values

    for (int[] row : table) {
        processRow(row);
    }
}

Upvotes: 2

jbranchaud
jbranchaud

Reputation: 6087

Here is an approach different than what most of the answers have provided so far. You can use a script (such as Bash) to generate the java file you are looking for. I wrote one really quickly that does exactly what you need:

#!/bin/bash

file="./ArrayGen.java";

echo "public class ArrayGen {" >> $file;

echo "public static void main(String[] args) {" >> $file;

for a in {0..999}
do

    echo "int[] arr$a = new int[200];" >> $file;

done

echo "} }" >> $file;

exit 0;

Upvotes: 0

esaj
esaj

Reputation: 16035

Sounds like what you really need is a Map:

Map<String, int[]> map = new HashMap<String, int[]>();

for(int i=0; i<1000; i++) {
    int[] arr = new int[200];
    for(int j=0; j<200; j++) {
        arr[j] = j;
    }
    map.put("Arr" + i, arr);
}

To access the values (in this case, the int -arrays), you use map.get(<key>), for example map.get("Arr10")

Upvotes: 4

jbranchaud
jbranchaud

Reputation: 6087

You are going to want to use a 2-dimensional array for this.

You can create a 2-dimensional array like so:

int[][] arr = new int[1000][200];
// iterates over the 1000 of first dimension
for(int i = 0; i < arr.length; i++) {

    // iterates over the 200 of the second dimension
    for(int j = 0; j < arr[i].length; j++) {
        // insert values into array here
    }
}

Upvotes: 0

Jon Egeland
Jon Egeland

Reputation: 12613

The best thing to do here would probably be an ArrayList of int[]s. Then you can add a new one whenever you want.

It would look like this:

ArrayList<int[]> arrs = new ArrayList<int[]>();
for(int i = 0; i < 1000; i++) {
  arrs.add(new int[200]);
  for(int j = 0; j < 200; j++) {
    arrs.get(i)[j] = j;
  }
}

Here is a reference for ArrayList.

Upvotes: 2

Related Questions