[英]In a java enhanced for loop, is it safe to assume the expression to be looped over will be evaluated only once?
In Java, a for-each loop. 在Java中,for-each循环。
If I have a method that generates an array, called genArray()
. 如果我有一个生成数组的方法,称为
genArray()
。
In the following code, will the array each time be re-generated by calling genArray()
? 在下面的代码中,每次都会通过调用
genArray()
重新生成数组吗? Or will Java call once the method and store a copy from the array? 或者Java会调用一次方法并从数组中存储一个副本吗?
for (String s : genArray())
{
//...
}
Thanks 谢谢
About the enhanced for statement , the Java Language Specifications writes: 关于增强的for语句 ,Java语言规范写道:
The enhanced for statement has the form:
增强的for语句具有以下形式:
EnhancedForStatement: for ( VariableModifiersopt Type Identifier: Expression) Statement
The Expression must either have type
Iterable
or else it must be of an array type (§10.1), or a compile-time error occurs.Expression必须具有
Iterable
,否则它必须是数组类型(第10.1节),否则会发生编译时错误。The scope of a local variable declared in the FormalParameter part of an enhanced
for
statement (§14.14) is the contained Statement在增强的
for
语句(第14.14节)的FormalParameter部分中声明的局部变量的范围是包含的StatementThe meaning of the enhanced
for
statement is given by translation into a basicfor
statement.增强的
for
语句的含义是通过翻译成一个基本的for
语句给出的。If the type of
Expression
is a subtype ofIterable
, then letI
be the type of the expression Expression.如果
Expression
的类型是Iterable
的子Iterable
,那么让I
成为表达式Expression的类型。iterator()
.iterator()
。 The enhancedfor
statement is equivalent to a basicfor
statement of the form:增强的
for
语句相当于表单的基本for
语句:for (I #i = Expression.iterator(); #i.hasNext(); ) { VariableModifiersopt Type Identifier = #i.next(); Statement }
Where
#i
is a compiler-generated identifier that is distinct from any other identifiers (compiler-generated or otherwise) that are in scope (§6.3) at the point where the enhanced for statement occurs.其中
#i
是编译器生成的标识符,它与发生增强for语句时的范围(第6.3节)中的任何其他标识符(编译器生成的或其他标识符)不同。Otherwise, the Expression necessarily has an array type,
T[]
.否则,Expression必须具有数组类型
T[]
。 LetL1 ... Lm
be the (possibly empty) sequence of labels immediately preceding the enhancedfor
statement.设
L1 ... Lm
是紧接在增强的for
语句之前的(可能是空的)标签序列。 Then the meaning of the enhanced for statement is given by the following basicfor
statement:然后,增强的for语句的含义是由下列基本给出
for
声明:T[] a = Expression; L1: L2: ... Lm: for (int i = 0; i < a.length; i++) { VariableModifiersopt Type Identifier = a[i]; Statement }
Where a and i are compiler-generated identifiers that are distinct from any other identifiers (compiler-generated or otherwise) that are in scope at the point where the enhanced for statement occurs.
其中a和i是编译器生成的标识符,它们与发生增强for语句的范围内的任何其他标识符(编译器生成的或其他标识符)不同。
So in your case, genArray()
doesn't return a subtype of Iterable
but an array type, so your enhanced for
statement is equivalent to the following basic for
statement: 所以你的情况,
genArray()
不返回的亚型Iterable
但数组类型,所以你增强for
语句等效于以下几个基本for
语句:
String[] a = genArray();
...
for (int i = 0; i < a.length; i++) {
String s = a[i];
// ...
}
And genArray()
will thus be called only once (but the currently accepted answer is partially wrong). 因此
genArray()
只会被调用一次(但目前接受的答案是部分错误)。
Java将调用genArray()一次,获取迭代器对象,并多次调用它。
It should only get used once - it's the same as calling this: 它应该只使用一次 - 它与调用它相同:
String[] strings = genArray();
for (String s : strings) {
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.