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
Rei7 Answers
New AnswerRei 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];
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
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]
~ 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!!
Sololearn Inc.
535 Mission Street, Suite 1591Send us a message