我认为增强的for循环只是一些语法糖,其行为与"传统"for循环完全相同.我在下面编写了一些代码,表明情况并非如此.
产出不同的原因是什么?或者我错过了什么,我应该停止以这种方式编写代码?
为什么不引用temp [(当前指数)]?对我来说,这确实是没有意义不
String temp[] = { "foo:bar" }; for (String t : temp) t = t.split(":", 2)[1]; System.out.println(temp[0].startsWith("foo")); //true for (int t = 0; t < temp.length; t++) temp[t] = temp[t].split(":", 2)[1]; System.out.println(temp[0].startsWith("foo")); //false
JB Nizet.. 7
foreach循环实际上相当于
for (int i = 0; i < temp.length; i++) String t = temp[i]; t = t.split(":", 2)[1]; }
这不会改变数组的任何元素.
foreach循环实际上相当于
for (int i = 0; i < temp.length; i++) String t = temp[i]; t = t.split(":", 2)[1]; }
这不会改变数组的任何元素.