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.
Java 3D array with array as elements - Stack Overflow
Help understanding 3D arrays in Java
java - Understanding Three-Dimensional Arrays - Stack Overflow
How to understand three dimensional array in Java - Stack Overflow
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?
Order doesn't matter, and in fact the former form is more readable:
final const int RED = 0;
final const int GREEN = 1;
final const int BLUE = 2;
int[][][] colorImage = new int[numRows][numColumns][3];
//...
int x = getSomeX();
int y = getSomeY();
int redComponent = colorImage[x][y][RED];
int greenComponent = colorImage[x][y][GREEN];
int blueComponent = colorImage[x][y][BLUE];
The order shouldn't matter, so one isn't more effective than the other. The only thing that matters is that whatever accesses colorImage knows which dimension is used for what. Bit more context on multidimensional arrays here.
There are no multidimensional arrays in java - just a syntax that looks and functions similarly; in reality they are arrays of arrays, resembling a tree structure more than a multidimensional array.
When you declare a three-dimensional array e.g. int[][][] myArray, you get actually just a reference variable myArray whos type is int[][][] (thats a type, just like String or Object). The only type assignment compatible with the first dimension is then int[][] and the only type assignment compatible with the second dimension is int[] and the last dimension is int (you see the pattern that just one set of [] is remove from left to right).
In practice:
int[] array1d = { 1, 2, 3 };
int[][] array2d = { array1d, { 4, 5, 6} };
int[][][] array3d = { array2d, { { 7, 8 }, { 9, 10, 11, 12 } } };
Both references as well as compatible declarations go happily into their appropiate slots. The third line also shows that dimensions need not be of equal size (contrary to classical multidimensional arrays). They don't even need to exist:
int[][] array2d_part = { { 1, 2, 3}, null };
Thats an array containing an array { 1, 2, 3 } at index 0 and a null at index 1. Since all except the rightmost dimension are reference types, null fits (null is compatible with any reference type).
If you imagine two dimensional array as matrix, then three dimensional would be a cube, filled with numbers.