Why this problem print 0 0 3 0 1? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why this problem print 0 0 3 0 1?

int[] a = {1, 2, 3, 4, 1}; for(int n:a) a[n]=0; for(int n:a) System.out.println(n);

16th Sep 2020, 7:45 PM
Juan Debenedetti
Juan Debenedetti - avatar
1 Answer
+ 3
It loops through the array values, not indexes. First step: n=1; a[1]=0; [1, 0, 3, 0 1] Second step: n=0; a[0]=0; [0, 0, 3, 0, 1] And so on
16th Sep 2020, 7:55 PM
Aleksandrs
Aleksandrs - avatar