0
can someone explain it to me?
public class Main { public static void main(String[] args) { int[]a = {1,2, 3, 4, 1}; for(int n: a){ a[n]=0; } for (int n:a){ System.out.println(n); } } }
3 Respostas
+ 3
Explanation in comments.
https://code.sololearn.com/c0pij3z7g868/?ref=app
+ 1
It sets certain elements in a to 0, but not all.
First iteration n=a[0]=1 sets a[1] to 0
Second iteration n=a[1]=0 (because a[1] was set to 0 in first iteration) sets a[0] to 0
And so on
The trick to understand is that updates are immediate
0
Thanks for this explanation i was thinking why it's not working as index a[0]=1
a[1]=2
a[2]=3
..