Initialising a multidimensional array in Java
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]]]"
Useful articles
- Java: Initializing a multidimensional array
- Java: Matrices and Multidimensional Arrays
Try replacing the appropriate lines with:
myStringArray[0][x-1] = "a string";
myStringArray[0][y-1] = "another string";
Your code is incorrect because the sub-arrays have a length of y
, and indexing starts at 0. So setting to myStringArray[0][y]
or myStringArray[0][x]
will fail because the indices x
and y
are out of bounds.
String[][] myStringArray = new String [x][y];
is the correct way to initialise a rectangular multidimensional array. If you want it to be jagged (each sub-array potentially has a different length) then you can use code similar to this answer. Note however that John's assertion that you have to create the sub-arrays manually is incorrect in the case where you want a perfectly rectangular multidimensional array.
You can also use the following construct:
String[][] myStringArray = new String [][] { { "X0", "Y0"},
{ "X1", "Y1"},
{ "X2", "Y2"},
{ "X3", "Y3"},
{ "X4", "Y4"} };
You can declare multi dimensional arrays like :
// 4 x 5 String arrays, all Strings are null
// [0] -> [null,null,null,null,null]
// [1] -> [null,null,null,null,null]
// [2] -> [null,null,null,null,null]
// [3] -> [null,null,null,null,null]
String[][] sa1 = new String[4][5];
for(int i = 0; i < sa1.length; i++) { // sa1.length == 4
for (int j = 0; j < sa1[i].length; j++) { //sa1[i].length == 5
sa1[i][j] = "new String value";
}
}
// 5 x 0 All String arrays are null
// [null]
// [null]
// [null]
// [null]
// [null]
String[][] sa2 = new String[5][];
for(int i = 0; i < sa2.length; i++) {
String[] anon = new String[ /* your number here */];
// or String[] anon = new String[]{"I'm", "a", "new", "array"};
sa2[i] = anon;
}
// [0] -> ["I'm","in","the", "0th", "array"]
// [1] -> ["I'm", "in", "another"]
String[][] sa3 = new String[][]{ {"I'm","in","the", "0th", "array"},{"I'm", "in", "another"}};
Multidimensional Array in Java
Returning a multidimensional array
Java does not truely support multidimensional arrays. In Java, a two-dimensional array is simply an array of arrays, a three-dimensional array is an array of arrays of arrays, a four-dimensional array is an array of arrays of arrays of arrays, and so on...
We can define a two-dimensional array as:
-
int[ ] num[ ] = {{1,2}, {1,2}, {1,2}, {1,2}}
-
int[ ][ ] num = new int[4][2]
num[0][0] = 1; num[0][1] = 2; num[1][0] = 1; num[1][1] = 2; num[2][0] = 1; num[2][1] = 2; num[3][0] = 1; num[3][1] = 2;
If you don't allocate, let's say
num[2][1]
, it is not initialized and then it is automatically allocated 0, that is, automaticallynum[2][1] = 0
; -
Below,
num1.length
gives you rows. -
While
num1[0].length
gives you the number of elements related tonum1[0]
. Herenum1[0]
has related arraysnum1[0][0]
andnum[0][1]
only. -
Here we used a
for
loop which helps us to calculatenum1[i].length
. Herei
is incremented through a loop.class array { static int[][] add(int[][] num1,int[][] num2) { int[][] temp = new int[num1.length][num1[0].length]; for(int i = 0; i<temp.length; i++) { for(int j = 0; j<temp[i].length; j++) { temp[i][j] = num1[i][j]+num2[i][j]; } } return temp; } public static void main(String args[]) { /* We can define a two-dimensional array as 1. int[] num[] = {{1,2},{1,2},{1,2},{1,2}} 2. int[][] num = new int[4][2] num[0][0] = 1; num[0][1] = 2; num[1][0] = 1; num[1][1] = 2; num[2][0] = 1; num[2][1] = 2; num[3][0] = 1; num[3][1] = 2; If you don't allocate let's say num[2][1] is not initialized, and then it is automatically allocated 0, that is, automatically num[2][1] = 0; 3. Below num1.length gives you rows 4. While num1[0].length gives you number of elements related to num1[0]. Here num1[0] has related arrays num1[0][0] and num[0][1] only. 5. Here we used a 'for' loop which helps us to calculate num1[i].length, and here i is incremented through a loop. */ int num1[][] = {{1,2},{1,2},{1,2},{1,2}}; int num2[][] = {{1,2},{1,2},{1,2},{1,2}}; int num3[][] = add(num1,num2); for(int i = 0; i<num1.length; i++) { for(int j = 0; j<num1[j].length; j++) System.out.println("num3[" + i + "][" + j + "]=" + num3[i][j]); } } }