Q&A Question about arrays. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Q&A Question about arrays.

What is the output of this code? int arr[ ] = new int[3]; for (int i = 0; i < 3; i++) { arr[i] = i; } int res = arr[0] + arr[2]; System.out.println(res); Can someone explain why this results in 2?

12th Jan 2019, 1:20 AM
Vital MC
1 Answer
+ 1
For the entire array, arr[i] = i; The array content/value is the index of the array, i.e. arr[0] is 0, arr[1] is 1, and so arr[0] + arr[2] is 2.
12th Jan 2019, 1:41 AM
Hatsy Rei
Hatsy Rei - avatar