Java怀旧:for-each version loop, Changes to iteration variable doesn't take effect
比如下面代码修改itr是无法影响到test_str_array的。
可以看成java compiler为我们dup了一个object出来,赋给了循环变量。
Example:
String test_str_array[] = new String[3];
test_str_array[0] = "Hello";
test_str_array[1] = "Welcome";
test_str_array[2] = "Nice to meet you";
for (String itr: test_str_array) {
itr = "Super119 hacked";
}
for (String itr: test_str_array) System.out.println(itr);
test_str_array[0] = "Hello";
test_str_array[1] = "Welcome";
test_str_array[2] = "Nice to meet you";
for (String itr: test_str_array) {
itr = "Super119 hacked";
}
for (String itr: test_str_array) System.out.println(itr);