0

Noob question regarding Multidimension Array

Hello! I would like to ask what is the use of myArr[0][2] = 42; in the code below (which is one of code example in Java's lesson)? 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); } } I have tried to remove myArr[0][2] = 42; and the output is still the same with or without, which made me curious what is the purpose?

7th Aug 2019, 4:42 PM
Rei
Rei - avatar
5 Answers
+ 3
Rei Please do not forget to give a checkmark, to the answer by ~ swim ~.
7th Aug 2019, 5:18 PM
Manual
Manual - avatar
+ 2
You changed [0][2] so it becomes. [1][4][42] [2] [6] [3] [7]
7th Aug 2019, 4:58 PM
Manual
Manual - avatar
+ 1
Rei Arrays contain values within given indexes. Your code prints [1],[0] which is 4 If the array is drawn as x by y. It starts like this. [1][4][5] [2] [6] [3] [7]
7th Aug 2019, 4:57 PM
Manual
Manual - avatar
+ 1
~ swim ~ Manual Thank you very much!! I understand what it means now and tried to print x with int x = myArr[0][2] and the value became to 42. Thank you very much for explaining in details!!
7th Aug 2019, 5:08 PM
Rei
Rei - avatar
+ 1
~ swim ~ Haha thats right, I got to axis' mixed up.😅
7th Aug 2019, 10:24 PM
Manual
Manual - avatar