**
The issue of for-each modify an array of values
**
Today write code, I noticed the problem can not be modified for-each array of values, as follows:
public class testForEach {
public static void main(String[] args) {
int[] a = new int[5];
for(int i:a) {
i=1;
}
System.out.println(Arrays.toString(a));
for(int i=0;i<a.length;i++) {
a[i]=1;
}
System.out.println(Arrays.toString(a));
}
}
Output:
[0, 0, 0, 0, 0]
[1, 1, 1, 1, 1]
As can be seen, for-each array value can not be modified
in fact:
for(int i:a) {
i=1;
}
equal
for(int i=0;i<a.length;i++) {
int b=a[i];
b=1;
}
So, just modify the value of the temporary variable, not modify the value inside the array of
conclusions: java in the for-each only through the array, the array of values can not be modified