在增强的for循环一个java,它是安全的假设除权pression要被环比将只计算一次? [英] In a java enhanced for loop, is it safe to assume the expression to be looped over will be evaluated only once?

查看:216
本文介绍了在增强的for循环一个java,它是安全的假设除权pression要被环比将只计算一次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

在以下code,将数组每次通过调用重新生成 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


  
  

防爆pression必须具有类型
  可迭代否则它必须是一个
  数组类型(§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部
  增强语句(§14.14)是
  所包含的声明

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

的含义增强
  声明被翻译成给出
  一个基本的语句。

如果类型的 防爆pression 的是
  的可迭代,然后让的 I 的是亚型
  恩pression的类型
  的防爆pression。中的iterator()。增强语句等价
  到的一个基本语句
  形式:

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.

否则,防爆pression必然
  有一个数组类型的 T [] 的。让我们的 L1 1 ... L M
  是的(可能为空)序列
  标签马上preceding的
  增强语句。然后,
  增强的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
}


  
  

在哪里产生的编译器的 A I
  标识符是任何不同
  其他标识符(编译器生成
  或以其它方式),这些范围在
  点增强的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()不返回可迭代的一个亚型,但数组类型,所以你增强语句等效于以下几个基本语句:

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).

这篇关于在增强的for循环一个java,它是安全的假设除权pression要被环比将只计算一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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