Initialisation of array !! | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Initialisation of array !!

public class Program { public static void main(String[] args) { int[ ][ ] myArr = { {1, 2, 3}, {4}, {5, 6, 7} }; myArr[0][2] = 42; int x = myArr[1][0]; System.out.println(x); } } in the above code myarr is 3 dimensions i.e., { {1, 2, 3}, {4}, {5, 6, 7} } define in that but in initialisation it accept just 2 brackets i.e., (int [][] myarr) instead of 3 !! why it is so ???

30th Aug 2017, 7:04 PM
SHUBHAM VERMA
SHUBHAM VERMA - avatar
3 Answers
+ 2
It's 2 dimensional not 3 {{1,2,3} ,{4} , {5,6,7}} This array represent this matrix👇🏻 1 2 3 4 0 0 5 6 7 3 curly brackets don't mean that this array is 3 dimensional But it tells you this 2 dimensional array have 3 row 3D array is contain two 2D array you can initialized like that: Int [][][] myArr ={{{1,2},{3,4}},{{5,6},{7,8}}} **Note the extra curly braces in 3D array
30th Aug 2017, 7:44 PM
Shade96
Shade96 - avatar
+ 6
The array myArr is two dimensional. A 3d array looks like this: 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} } }; Example taken from Stackoverflow, because I'm too lazy to type it all in ^^ https://stackoverflow.com/questions/23630668/java-3d-array-assign-values
30th Aug 2017, 7:28 PM
Tashi N
Tashi N - avatar
+ 2
Right, 2 dimensions. width & height. 1,2,3 4 5,6,7
30th Aug 2017, 7:29 PM
Tim
Tim - avatar