Here is one way to do it. It is a "3D" array. For an array of [R][C][Other]
- the first level
Ris the number of rows. - the second is
Cthe number of columns. - the third is the size of the array in each
[r][c]cell.
int ASize = 20;
int[][][] loc = new int[5][5][2];
for (int size = 0, xy = ASize / 2; size < 5;
size++, xy += ASize) {
for (int size2 = 0, xy2 = ASize / 2; size2 < 5;
size2++, xy2 += ASize) {
loc[size][size2][0] = xy2;
loc[size][size2][1] = xy;
}
}
for (int[][] arr : loc) {
System.out.println(Arrays.deepToString(arr));
}
Prints
[[10, 10], [30, 10], [50, 10], [70, 10], [90, 10]]
[[10, 30], [30, 30], [50, 30], [70, 30], [90, 30]]
[[10, 50], [30, 50], [50, 50], [70, 50], [90, 50]]
[[10, 70], [30, 70], [50, 70], [70, 70], [90, 70]]
[[10, 90], [30, 90], [50, 90], [70, 90], [90, 90]]
This prints as follows.
- Multiple dimensions in Java are really arrays of arrays.
- so the loop iterates over each row (which is a
[5][2]array) and prints each row usingArrays.deepToString() Arrays.toString()is normally used to print arrays but only the first level.
Videos
Here is one way to do it. It is a "3D" array. For an array of [R][C][Other]
- the first level
Ris the number of rows. - the second is
Cthe number of columns. - the third is the size of the array in each
[r][c]cell.
int ASize = 20;
int[][][] loc = new int[5][5][2];
for (int size = 0, xy = ASize / 2; size < 5;
size++, xy += ASize) {
for (int size2 = 0, xy2 = ASize / 2; size2 < 5;
size2++, xy2 += ASize) {
loc[size][size2][0] = xy2;
loc[size][size2][1] = xy;
}
}
for (int[][] arr : loc) {
System.out.println(Arrays.deepToString(arr));
}
Prints
[[10, 10], [30, 10], [50, 10], [70, 10], [90, 10]]
[[10, 30], [30, 30], [50, 30], [70, 30], [90, 30]]
[[10, 50], [30, 50], [50, 50], [70, 50], [90, 50]]
[[10, 70], [30, 70], [50, 70], [70, 70], [90, 70]]
[[10, 90], [30, 90], [50, 90], [70, 90], [90, 90]]
This prints as follows.
- Multiple dimensions in Java are really arrays of arrays.
- so the loop iterates over each row (which is a
[5][2]array) and prints each row usingArrays.deepToString() Arrays.toString()is normally used to print arrays but only the first level.
While defining a nested array you have to use a pair of square brackets [] for each level of nesting. And for "3d" array it would be [][][].
Note that there's actually no "2d" or "3d" array in Java. We can create a nested array, i.e. an array that contains other arrays.
While creating a nested array, only the length of the outer (enclosing) array needs to be provided. And you can omit declaring the sizes of the inner arrays, that can useful when inner arrays may differ in length.
I.e. this declaration will also be valid:
int[][][] loc = new int[5][][];
Another important thing about arrays that you need to be aware that although they are objects and inherit all behavior from the Object class, array don't have their own classes. As a consequence of this, if you invoke toString(), hasCode() and equals() on an array, a default implementation from the Object class would be invoked. Therefore, an attempt to print an array will result in strange symbols appearing on the console.
For that reason, Arrays utility class if your friend when need to print or compare arrays.
public static void main(String[] args) {
int[][][] loc = new int[5][5][2];
for (int row = 0; row < loc.length; row++) {
for (int col = 0; col < loc[row].length; col++) {
loc[row][col][0] = (1 + 2 * row) * 10;
loc[row][col][1] = (1 + 2 * col) * 10;
}
}
print3DArray(loc);
}
public static void print3DArray(int[][][] arr) {
for (int row = 0; row < arr.length; row++) {
for (int col = 0; col < arr[row].length; col++) {
System.out.print(Arrays.toString(arr[row][col]) + " ");
}
System.out.println();
}
}
Output:
[10, 10] [10, 30] [10, 50] [10, 70] [10, 90]
[30, 10] [30, 30] [30, 50] [30, 70] [30, 90]
[50, 10] [50, 30] [50, 50] [50, 70] [50, 90]
[70, 10] [70, 30] [70, 50] [70, 70] [70, 90]
[90, 10] [90, 30] [90, 50] [90, 70] [90, 90]
I understand how to initialize and iterate though 2D arrays, but I'm confused with 3D arrays.
Can someone give an example of how to initialize 3D array with different numbers and then show how to iterate through it?