3

I need a large series of arrays, right now I am creating them manually:

int[] row1 = new int[3];
int[] row2 = new int[3];
int[] row3 = new int[3];

I would like to create these in an array, something like this:

public final int SIZE = 3;
for (int i = 1;i <= 3;i++)  
  int[] row[i] = new int[3];

But I am not sure how to generate arrays in a loop.

How do I do this?

Specifically, how do I generate dynamic identifiers on each iteration?

4
  • 2
    Why not combinedly make a 2d-Array? Commented Nov 7, 2014 at 21:10
  • You shouldn't need dynamic identifiers. Make an array of arrays and assign the array you make to the next position of that array. This would work with any data structure (linked list, array list, etc) Commented Nov 7, 2014 at 21:11
  • You might want to always use braces Commented Nov 7, 2014 at 21:12
  • How would that work? You can't access Int[] row1 ... by addressing row[1]. The answer to your question if it was posed in other words would probably be int[][] rows = int[numberOfRows]{sizeOfEachRow]; Commented Nov 7, 2014 at 21:12

5 Answers 5

13

Specifically, how do I generate dynamic identifiers on each iteration?

You don't. Instead, you realize that you've actually got a collection of arrays. For example, you could have:

int[][] rows = new int[3][3];

Or:

List<int[]> rows = new ArrayList<>();
for (int i = 0; i < 3; i++) {
    rows.add(new int[3]);
}
Sign up to request clarification or add additional context in comments.

Comments

2

Arrays always start at index 0, so for (int i = 1;i <= 3;i++) is wrong twice. Once for 1 and once for not using SIZE. You make a multidimensional array like,

public final int SIZE = 3;  
int[][] tables = new int[SIZE][SIZE];

Note that every value in tables is 0. You could use Arrays.fill(int[],int) to give every element a single value,

Arrays.fill(tables, 1);
// [[1, 1, 1], [1, 1, 1], [1, 1, 1]]

Or, you might use a pair of for loops like

for (int i = 0; i < tables.length; i++) {
  for (int j = 0; j < tables[i].length; j++) {
    tables[i][j] = (tables[i].length * i) + j;
  }
}
// [[0, 1, 2], [3, 4, 5], [6, 7, 8]]

And you can also use a literal assignment and you might use Arrays.deepToString(Object[]) to display your multi-dimensional arrays like,

int[][] tables = { { 8, 7, 6 }, { 5, 4, 3 }, { 2, 1, 0 } };
// [[8, 7, 6], [5, 4, 3], [2, 1, 0]]
System.out.println(Arrays.deepToString(tables));

Comments

1

Simply make it a 2D-array. Proceed as follows :-

public final int SIZE = 3;
int row[3][SIZE] = new int[3][SIZE];

Comments

1

Here you would use an array of arrays, a 2-dimensional array.

public final int SIZE = 3;
int[][] array = new int[SIZE][SIZE];

This way you don't need dymanic identifiers, you can just use an index. You can access one element like this:

// i is the index of the array, j is the index of the element
int element = array[i][j];

Comments

1

Based on asking specifically about arrays, you may not be interested in more bloated types. But if that's not an issue, you could use a Map. And put each array into the map with a unique identifier as the key.

Map myRows = new HashMap <String, int[]>();
myRows.put("row1", new int[] {1,2,3});

note: you could easily replace the String key with an Integer key.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.