What's the use of " myArr[0][2] = 42;" in this code , if we removed it the code will give same result | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What's the use of " myArr[0][2] = 42;" in this code , if we removed it the code will give same result

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); } }

28th Sep 2018, 9:00 PM
hend kandil
hend kandil - avatar
2 Answers
+ 4
It changes the array by replacing the 3 (third value in the first group) with 42. Then you print the variable x, which is the first value in the second group (4). That's why you don't see any changes. You're changing one variable but printing a different variable. If you print the whole array, you'll see {{1, 2, 42}, {4}, {5, 6, 7}}.
29th Sep 2018, 4:55 AM
Anna
Anna - avatar
+ 2
It doesn't really have a purpose in this context, but if you printed [0][2] before changing it to 42, and then print it again after, it would.
29th Sep 2018, 3:19 AM
Zeke Williams
Zeke Williams - avatar