+ 1

How does it calculate?

myarr[0][2]= 42; how? why we write this? 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); } }

2nd Nov 2016, 7:12 PM
ehsan shahbazi
ehsan shahbazi - avatar
1 Answer
+ 2
0 is the first element in the indexing, so myarr [0] is {1,2,3} which is the first element in the myarr list(array) and myarr[0][2] is 3 in {1,2,3} myArr[0][2] = 42 sets the 3. element whose value is 3 at first, to 42 So {1,2,3} becomes {1,2,42}, remember [0] is the first elemnt, [1] is second and [2] is third
2nd Nov 2016, 7:43 PM
Ven Zallow
Ven Zallow - avatar