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];
Answer from Strelok on Stack OverflowHelp understanding 3D arrays in Java
java - Understanding Three-Dimensional Arrays - Stack Overflow
How to understand three dimensional array in Java - Stack Overflow
Java 3D array with array as elements - Stack Overflow
Videos
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.
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]
static String[][][] School= new String[1000][20][5];
Consider figure which has 3 Dimension.
So when you insert School[0][0][0]="A1" it means you have entered element at 0,0,0 position.
From 0,0,0 this will move upto the position 1000,20,5.
You can insert like this But you have so many elements.
School[0][0][0]="A1"
School[0][0][1]="A2"
School[0][0][2]="A3"
.....
School[0][1][0]="B1"
School[0][1][1]="B2"
School[0][1][2]="B3"
......
In 3D array elements look like
int[3][4][2] array3D
// means Three (4x2) 2 Dimensional Arrays
int[4][2]
//means Four 1 dimensional arrays.
Now how to add elements in 3D array?
At Start you can directly use
int[][][] threeDArray =
{ { {1, 2, 3}, { 4, 5, 6}, { 7, 8, 9} },
{ {10, 11, 12}, {13, 14, 15}, {16, 17, 18} },
{ {19, 20, 21}, {22, 23, 24}, {25, 26, 27} } };
This is very tedious task in your case as you want to insert details at every position.
As you have 1000 records.
Your array will have elements like this

NOTE:It's not recommended to use 3D array for this purpose.
Suggestion:Declare a class with three Strings create constructor with this three parameters and put getter and setters to get and set values via Objects
I will suggest instead of using a 3D array, you shall create a Student Class that will hold all the information for a student and A Class for SchoolClass that will hold a list of students in the class and name of class and you can maintain an Array of SchoolClass to serve the purpose.
This way you will be able to manage it better.
Hope this helps
I could write a program using either system, however 3 one dimensional arrays would be a little simpler to code. If one 3 dimensional Array was a lot more memory efficient i would put in the extra effort to use that.