2

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

1
  • Is it necessary that each array have the name Arr# (where # is the number) or can you implement an ArrayList approach like Jon suggested? I can't imagine what specification you are meeting by requiring that the arrays have particular names. Commented Feb 11, 2012 at 20:11

5 Answers 5

4

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")

Sign up to request clarification or add additional context in comments.

3 Comments

Don't forget to mark this as the answer so others can see it at the top of this post in the future!
as a slight updated, the map.get("Arr10") does not work (simply says Arr10 cannot be resolved to a variable). I am trying to figure this out but havent worked with maps before so if anyone has any suggestions that would be great.
Sounds like you're trying to use a variable name Arr10, and haven't declared one. If you have the quotes (") around the string, like this: "Arr10", it's a String literal, and should work. If you want to use a variable, use a String (because that's what the type of the key is declared to the map): String arrName = ""; arrName = "Arr" + 10; map.get(arrName)
2

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.

Comments

2

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);
    }
}

Comments

0

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
    }
}

4 Comments

I am aware that a 2d array would be much simpler but for my purposes it is necessary to have a series of 1d arrays
What is it that necessitates that?
Requirement for an external library I am trying to use
Okay, well consider the my other answer I posted using a script.
0

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;

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.