在 Java 增强的 for 循环中,假设要循环的表达式只计算一次是否安全? [英] In a java enhanced for loop, is it safe to assume the expression to be looped over will be evaluated only once?

查看:33
本文介绍了在 Java 增强的 for 循环中,假设要循环的表达式只计算一次是否安全?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Java 中,一个 for-each 循环.
如果我有一个生成数组的方法,称为 genArray().

In Java, a for-each loop.
If I have a method that generates an array, called genArray().

在下面的代码中,每次调用genArray()都会重新生成数组吗?或者Java会调用一次该方法并从数组中存储一个副本?

In the following code, will the array each time be re-generated by calling genArray()? Or will Java call once the method and store a copy from the array?

for (String s : genArray())
{
    //...
}

谢谢

推荐答案

关于 增强的 for 语句,Java 语言规范写道:

About the enhanced for statement, the Java Language Specifications writes:

增强的 for 语句具有形式:

The enhanced for statement has the form:

EnhancedForStatement:
        for ( VariableModifiersopt Type Identifier: Expression) Statement

表达式必须具有类型Iterable 否则它必须是一个数组类型(第 10.1 节),或编译时发生错误.

The Expression must either have type Iterable or else it must be of an array type (§10.1), or a compile-time error occurs.

声明的局部变量的作用域在一个的 FormalParameter 部分增强的 for 语句(第 14.14 节)是包含的声明

The scope of a local variable declared in the FormalParameter part of an enhanced for statement (§14.14) is the contained Statement

增强for的含义陈述是通过翻译成一个基本的 for 语句.

The meaning of the enhanced for statement is given by translation into a basic for statement.

如果 Expression 的类型是一个Iterable 的子类型,然后让 I 成为表达式的类型表达式.iterator().增强的 for 语句是等效的到基本的 for 语句形式:

If the type of Expression is a subtype of Iterable, then let I be the type of the expression Expression.iterator(). The enhanced for statement is equivalent to a basic for statement of the form:

for (I #i = Expression.iterator(); #i.hasNext(); ) {

        VariableModifiersopt Type Identifier = #i.next();
   Statement
}

其中 #i 是编译器生成的与任何标识符不同的标识符其他标识符(编译器生成的或其他)在范围内(第 6.3 节)在增强的点语句发生.

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.

否则,表达式必然有一个数组类型,T[].让 L1 ... Lm是(可能是空的)序列紧接在前面的标签增强的 for 语句.那么增强的 for 语句的含义由以下基本 for 给出声明:

Otherwise, the Expression necessarily has an array type, T[]. Let L1 ... Lm be the (possibly empty) sequence of labels immediately preceding the enhanced for statement. Then the meaning of the enhanced for statement is given by the following basic for statement:

T[] a = Expression;
L1: L2: ... Lm:
for (int i = 0; i < a.length; i++) {
        VariableModifiersopt Type Identifier = a[i];
        Statement
}

其中 ai 是编译器生成的与任何标识符不同的标识符其他标识符(编译器生成的或其他)在范围内增强 for 语句的点发生.

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.

因此,在您的情况下,genArray() 不返回 Iterable 的子类型,而是返回数组类型,因此您增强的 for 语句等价于以下基本的 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:

String[] a = genArray();
...
for (int i = 0; i < a.length; i++) {
    String s = a[i];
    // ...
}

因此 genArray() 只会被调用一次(但当前接受的答案是部分错误的).

And genArray() will thus be called only once (but the currently accepted answer is partially wrong).

这篇关于在 Java 增强的 for 循环中,假设要循环的表达式只计算一次是否安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆