【发布时间】:2017-04-18 21:45:21
【问题描述】:
在下面的代码中,我预计当我们遍历 5 个数组引用时,我们可以在循环中重新分配这些引用,因此最后应该有 5 个非空引用。
int[][] scores = new int[5][];
for (int[] sr : scores){
System.out.println(sr); // 5 null entries are printed out as expected
}
for (int[] sr : scores){
sr = new int[2]; // the 5 null references are assigned to a new array
}
for (int[] sr : scores){
System.out.println(sr); // 5 null entries are still printed out...
}
【问题讨论】:
-
sr 是一个临时变量,您不能使用它进行更新!
-
我看到所以 sr 是参考的副本,而不是实际的参考。因此重定向副本不会影响原始数组。
标签: java for-loop reference iterator