What is the correct way to declare a multidimensional array and assign values to it?
This is what I have:
int x = 5;
int y = 5;
String[][] myStringA
Java doesn't have "true" multidimensional arrays.
For example, arr[i][j][k]
is equivalent to ((arr[i])[j])[k]
. In other words, arr
is simply an array, of arrays, of arrays.
So, if you know how arrays work, you know how multidimensional arrays work!
Declaration:
int[][][] threeDimArr = new int[4][5][6];
or, with initialization:
int[][][] threeDimArr = { { { 1, 2 }, { 3, 4 } }, { { 5, 6 }, { 7, 8 } } };
Access:
int x = threeDimArr[1][0][1];
or
int[][] row = threeDimArr[1];
String representation:
Arrays.deepToString(threeDimArr);
yields
"[[[1, 2], [3, 4]], [[5, 6], [7, 8]]]"