+ 4

Multidimensional Arrays

int array[ ][ ] = {{3, 5, 8}, {7, 54, 1, 12, 4}}; System.out.println(array[0][2]); * how come that the output of this code is 8? while the output of the code below is 4? int[ ][ ] myArr = { {1, 2, 3}, {4}, {5, 6, 7} }; myArr[0][2] = 42; int x = myArr[1][0]; // 4 Can anyone explain this please? It's a bit confusing. Lol!

6th Dec 2016, 5:43 AM
Lyka O. Matilla
Lyka O. Matilla - avatar
3 ответов
+ 2
array[0][2] ask the first array in your array which is {3,5,8}. Then it asks for the third element which is 8. myArr[0][2] sets the third value from the first array in myArr to 42. myArr[1][0] asks for the second array of myArr and then for the first value. Which is 4. Remember Arrays are indexed by Zero. So the first element is on position 0, the second on 1... and the last one of qn array of length n is on position n-1
6th Dec 2016, 7:18 AM
Andreas K
Andreas K - avatar
0
array[0][2] means [0] indicats rows because it is first subscript and [2] indicats column because it is second subscript according to this you can find your matix location and can print that element now draw your matrix and feel free to print any element...... {3 5 8} | 0,0 0,1 0,2 | {4 2 6 7} | 1,0 1,1 1,2 1,3 |
11th Feb 2017, 7:22 PM
Abhishek Jangid
Abhishek Jangid - avatar
0
9th May 2020, 8:56 AM
Thirunavakarasu
Thirunavakarasu - avatar