【发布时间】:2010-10-10 00:28:36
【问题描述】:
下面的代码没有达到我的预期。这段代码执行后,每个字符串都为空。
String[] currentState = new String[answer.length()];
for(String x : currentState)
{
x = "_";
}
下面的代码符合我的预期。 currentState 中的每个字符串现在都是“_”
String[] currentState = new String[answer.length()];
for (int i = 0; i < currentState.length; i++) {
currentState[i] = "_";
}
有人可以解释为什么第一种情况不起作用吗?
【问题讨论】:
-
想必你的意思是:String[] currentState = new String[answer.length()];