+ 2

What am I missing? This gets me on SoloLearn Java challenges everytime!

Is this not asking to print the int at index 2 of the array? Why is correct answer: 4 ? What is the output of this code? int[] arr = {1,2,3,4}; int x = arr[2]; System.out.print(arr[x]);

21st Sep 2019, 5:00 AM
will
will - avatar
2 ответов
+ 3
Ace 🤦‍♂️ Doh! Assigning int x the value of index 2 , which is 3, then printing the int at index 3, which is 4 ! Got me a bunch of times, I kept thinking it just wanted to print int at index 2 --- Thank you for breaking it down!
21st Sep 2019, 5:26 AM
will
will - avatar