Q:

Can anyone please explain how this is working? 

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

This for each loop showing something unexpected result when I make a all array elements 0.Instead of showing all '0' it is giving output something like '00301'. Not able to understand how this is working?  

A:

a[n] = 0; is setting the a[some_value_from_the_array] to 0, not setting all the array elements to 0.