Java foreach 对原语的迭代顺序是否精确定义?

发布于 2024-07-15 10:59:46 字数 381 浏览 7 评论 0原文

示例代码:

int a[] = new int[]{0, 1, 2, 3};
int result = 0;
for (int i : a)
    result += i;

循环是否保证迭代 a[0]a[1]a[2]a [3] 按这个顺序? 我坚信答案是肯定的,但是此页面< /a> 似乎没有明确说明顺序。

有可靠的参考吗?

Example code:

int a[] = new int[]{0, 1, 2, 3};
int result = 0;
for (int i : a)
    result += i;

Is the loop guaranteed to iterate across a[0], a[1], a[2], a[3] in that order? I strongly believe the answer is yes, but this page seems to not unambiguously state order.

Got a solid reference?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

爱情眠于流年 2024-07-22 10:59:47

它在 JLS< /a> 那:

for ( VariableModifiersopt Type Identifier: Expression) Statement

相当于

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

It states in the JLS that:

for ( VariableModifiersopt Type Identifier: Expression) Statement

is equivalent to

T[] a = Expression;
L1: L2: ... Lm:
for (int i = 0; i < a.length; i++) {
        VariableModifiersopt Type Identifier = a[i];
        Statement
}
彼岸花ソ最美的依靠 2024-07-22 10:59:47

我在您引用的页面中没有找到任何暗示无序迭代的内容。 可以贴一下具体报价吗?

无论如何,我发现这段代码:

public static void main( String args[] ) {
    double a[] = new double[] { 0, 1, 2, 3 };
    int result = 0;
    for ( double i : a ) {
        result += i;
    }

反编译为旧式循环:

 public static void main(String args[])
    {
        double a[] = {
            0.0D, 1.0D, 2D, 3D
        };
        int result = 0;
        double ad[];
        int k = (ad = a).length;
        for(int j = 0; j < k; j++)
        {
            double i = ad[j];
            result = (int)((double)result + i);
        }
    }

当然,这与保证不同,但至少对数组进行无序迭代会非常奇怪,并且似乎违背明显的常识性实施。

I did not find anything in the page you've referenced that would imply out-of-order iteration. Can you post the specific quote?

In any case, I find that this code:

public static void main( String args[] ) {
    double a[] = new double[] { 0, 1, 2, 3 };
    int result = 0;
    for ( double i : a ) {
        result += i;
    }

decompiles to old-style looping:

 public static void main(String args[])
    {
        double a[] = {
            0.0D, 1.0D, 2D, 3D
        };
        int result = 0;
        double ad[];
        int k = (ad = a).length;
        for(int j = 0; j < k; j++)
        {
            double i = ad[j];
            result = (int)((double)result + i);
        }
    }

Of course, that's not the same as a guarantee, but at the very least out-of-order iteration over an array would be very weird and would seem to go against obvious common-sense implementation.

書生途 2024-07-22 10:59:46

根据JLS,增强的for 语句,你的 for 循环相当于

int[] array = a;
for (int index = 0; index < a.length; index++) {
    int i = array[index];
    result += i;
}

“其中 arrayindex 是编译器生成的标识符,它们与任何其他标识符不同(编译器生成的或其他)在增强 for 语句发生处的范围内。” (这里稍微解释一下变量名)。

所以是的:订单绝对有保证。

According to the JLS, The enhanced for statement, your for-loop is equivalent to

int[] array = a;
for (int index = 0; index < a.length; index++) {
    int i = array[index];
    result += i;
}

"where array and index 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." (slightly paraphrasing the variable names here).

So yes: the order is absolutely guaranteed.

筱武穆 2024-07-22 10:59:46

请参阅 Java 语言规范第三版的 第 14.14.2 节

如果表达式的类型是子类型
的 Iterable,那么让 I 成为以下类型
表达式Expression.iterator()。
增强的 for 语句是
相当于基本的 for 语句
形式:

for (I #i = Expression.iterator(); #i.hasNext(); ) { 
          VariableModifiersopt 类型标识符 = #i.next(); 
     陈述 
  } 
  

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

See section 14.14.2 of the Java Language Specification, 3rd edition.

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
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文