Noob question regarding Multidimension Array | Sololearn: Learn to code for FREE!

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?

8/7/2019 4:42:29 PM

Rei

7 Answers

New Answer

+3

Rei myArr[0][2] = 42 means assigning a numerical value to 3rd column in row 0, remember indexing starts from 0 You are printing the value of myArr[1][0] i.e value of 1st (0th) column in 2nd row print the value of myArr[0][2];

+3

Manual Shouldn't the array representation be like this [1] [2] [3] [4] [5] [6] [7] instead of what you have drawn? else value [3] should have been overwritten with 42, not 5

+3

Rei Please do not forget to give a checkmark, to the answer by ~ swim ~.

+2

You changed [0][2] so it becomes. [1][4][42] [2] [6] [3] [7]

+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]

+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!!

+1

~ swim ~ Haha thats right, I got to axis' mixed up.ðŸ˜…