比如下面代码修改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);

相关文章:

  • 2021-10-08
  • 2022-12-23
  • 2021-10-12
  • 2021-11-24
  • 2021-06-28
  • 2022-01-07
猜你喜欢
  • 2021-10-05
  • 2022-02-23
  • 2022-12-23
  • 2021-07-14
  • 2021-12-15
  • 2021-08-25
  • 2021-10-17
相关资源
相似解决方案