int[ ][ ] myArr = { {1, 2, 3}, {4}, {5, 6, 7} }; why are there only 2 [ ] in the array declaration? | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
+ 1

int[ ][ ] myArr = { {1, 2, 3}, {4}, {5, 6, 7} }; why are there only 2 [ ] in the array declaration?

multi dimentional arrays

21st Jan 2018, 11:40 AM
russ
5 Antworten
+ 24
https://www.sololearn.com/learn/Java/2148/?ref=app https://www.sololearn.com/learn/Java/2149/?ref=app 1d array ... set of values 2d array ... set of 1d array 3d array ... set of 2d array //mine definitions 😂
21st Jan 2018, 11:45 AM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 12
No. Dimensions do not concern "how many arrays" you have. Dimensions are about how many levels your array is nested within. In {{1, 2, 3}, {4}, {5, 6, 7}}, arr[0] would refer to {1, 2, 3} arr[1] would refer to {4} arr[2] would refer to {5, 6, 7} arr[0][0] would refer to 1, which is the first element in the first array within the multidimensional array. Similarly : arr[0][1] would refer to 2 arr[1][0] would refer to 4 arr[2][1] would refer to 6 You would need three dimensions when you have: {{1, 2, {3, 4}}, {5}, {6, 7}} Note that {3, 4} is within {1, 2, {3, 4}}, and is represented by arr[0][2]. To access 4, for example, would be arr[0][2][1].
21st Jan 2018, 2:30 PM
Hatsy Rei
Hatsy Rei - avatar
+ 10
Because specifying two dimensions would be enough to identify each element in the array, as it is.
21st Jan 2018, 12:27 PM
Hatsy Rei
Hatsy Rei - avatar
0
so if you had 4 arrays you would need 3 sets of brackets etc ?
21st Jan 2018, 2:00 PM
russ
0
the simple answer is it is a 2 dimensional array if explained further, [] means 1 dimension [] [] means 2 dimensions when we need to reference an element in a 2 dimensional array we use 2 indexes e.g :- int x=myarr[0][1] //x=2 think of 2 dimensional array as a table × 0 1 2 - X 0 1 4 5 1 2 6 2 3 7 -Y X- array numbers (1st array gets 0, 2nd array gets 1.. and so on) Y- index of each array
21st Jan 2018, 3:13 PM
vinod mahes
vinod mahes - avatar