理解 Java 中的每个循环
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/589433/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Understanding for each loop in Java
提问by John Leidegren
The code below doesn't do what I expect. Every string is null after this code executes.
下面的代码不符合我的预期。此代码执行后,每个字符串都为空。
String[] currentState = new String[answer.length()];
for(String x : currentState)
{
x = "_";
}
The code below does what I expect. Every string in currentState is now "_"
下面的代码符合我的预期。currentState 中的每个字符串现在都是“_”
String[] currentState = new String[answer.length()];
for (int i = 0; i < currentState.length; i++) {
currentState[i] = "_";
}
Can someone explain why the first case doesn't work?
有人可以解释为什么第一种情况不起作用吗?
回答by John Leidegren
By design the for each variable 'x' (in this case) is not meant to be assigned to. I'm surprised that it even compiles fine.
通过设计,每个变量 'x'(在这种情况下)并不打算分配给。我很惊讶它甚至可以很好地编译。
String[] currentState = new String[answer.length()];
for (String x : currentState) {
x = "_"; // x is not a reference to some element of currentState
}
The following code maybe shows what you're in effect are doing. Note that this is not how enumerations work but it exemplifies why you can't assign 'x'. It's a copy of the element at location 'i'. (Edit: note that the element is a reference type, as such it's a copy of that reference, assignment to that copy does not update the same memory location i.e. the element at location 'i')
以下代码可能会显示您实际上在做什么。请注意,这不是枚举的工作方式,但它举例说明了为什么不能分配“x”。它是位置“i”处元素的副本。(编辑:请注意,该元素是一个引用类型,因此它是该引用的副本,对该副本的赋值不会更新相同的内存位置,即位置“i”处的元素)
String[] currentState = new String[answer.length()];
for (int i = 0; i < answer.length(); i++) {
String x = currentState[i];
x = "_";
}
回答by TofuBeer
Original code:
原始代码:
String currentState = new String[answer.length()];
for(String x : currentState)
{
x = "_";
}
Rewritten code:
重写代码:
String currentState = new String[answer.length()];
for(int i = 0; i < currentState.length; i++)
{
String x;
x = currentState[i];
x = "_";
}
How I would write the code:
我将如何编写代码:
String currentState = new String[answer.length()];
for(final String x : currentState)
{
x = "_"; // compiler error
}
Rewritten code with the error:
用错误重写代码:
String currentState = new String[answer.length()];
for(int i = 0; i < currentState.length; i++)
{
final String x;
x = currentState[i];
x = "_"; // compiler error
}
Making the variables final highlights when you do things like this (it is a common beginner mistake). Try to make all of your variables final (instance, class, arguments, exceptions in catch. etc...) - only make them non-final if you really have to change them. You should find that 90%-95% of your variables are final (beginners will wind up with 20%-50% when they start doing this).
当你做这样的事情时,让变量最终突出显示(这是一个常见的初学者错误)。尝试使所有变量成为最终变量(实例、类、参数、catch 中的异常等...) - 只有在您确实必须更改它们时才将它们设为非最终变量。您应该会发现 90%-95% 的变量是最终变量(初学者在开始这样做时会得到 20%-50%)。
回答by oxbow_lakes
Because xis a reference (or a variable of reference-type). All the first piece of code does is re-point the reference at a new value. For example
因为x是引用(或引用类型的变量)。第一段代码所做的就是将引用重新指向一个新值。例如
String y = "Jim";
String x = y;
y = "Bob";
System.out.println(x); //prints Jim
System.out.println(y); //prints Bob
The fact that you are re-assigning the reference yto "Bob" does not affect what the reference xwas assigned to.
您将引用重新分配y给“Bob”这一事实不会影响引用x分配给的内容。
回答by Luke
You can convert your array to a List and then iterate like this:
您可以将数组转换为 List,然后像这样迭代:
String[] currentState = new String[answer.length()];
List<String> list = Arrays.asList(currentState);
for(String string : list) {
x = "_";
}
回答by Luke
Object x[]={1,"ram",30000f,35,"account"}; for(Object i:x) System.out.println(i); for each is used for sequential access
对象 x[]={1,"ram",30000f,35,"account"}; for(Object i:x) System.out.println(i); for each 用于顺序访问
回答by Alexander Smirnov
The for each loop meant for this:
for each 循环意味着:
List suits = ...;
List ranks = ...;
List sortedDeck = new ArrayList();
for (Suit suit : suits){
for (Rank rank : ranks)
sortedDeck.add(new Card(suit, rank));
}
so consider above you can do this:
所以考虑上面你可以这样做:
String[] currentState = new String[answer.length()];
List<String> buffList = new ArrayList<>();
for (String x : currentState){
x = "_";
buffList.add(x);
// buffList.add(x = "_" ); will be work too
}
currentState = buffList.toArray(currentState);

