使用每个循环的“高级”在 Java 中初始化数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2556419/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 09:14:09  来源:igfitidea点击:

Initializing an array in Java using the 'advanced' for each loop

javaforeachfor-loop

提问by Corleone

Is it possible to initialise an array in Java using the 'advanced' for loop?

是否可以使用“高级”for 循环在 Java 中初始化数组?

e.g.

例如

    Integer[ ] numbers = new Integer[20];
    int counter = 0;
    for ( Integer i : numbers )
    {
        i = counter++;
    }

    for ( Integer i : numbers )
    {
        System.out.println(i);
    }

This prints all nulls, why is that?

这将打印所有空值,这是为什么?

采纳答案by Mark Byers

No, because you aren't assigning to the array, you are assigning to the temporary variable called i. The array doesn't see the change.

不,因为您不是分配给数组,而是分配给名为i. 阵列看不到变化。

The following shows roughly equivalent code using the normal forloop. This should make it easier to see why it fails to update the array:

下面显示了使用普通for循环的大致等效代码。这应该可以更容易地看出为什么它无法更新阵列:

for (int j = 0; j < numbers.length; j++) { 
    Integer i = arr[j]; // i is null here.
    i = counter++; // Assigns to i. Does not assign to the array.
}

回答by Thomas Pornin

Basically no, not as you wish. In the 'advanced' for loop, there is no way to access the hidden counter, and neither is there to perform a writeaccess on the corresponding array slot.

基本上没有,不是如你所愿。在“高级”for 循环中,无法访问隐藏计数器,也无法对相应的数组槽执行访问。

回答by Romain

In your case, you cannot. For-each hides the iterator on the underlying collection, so here you cannot figure out what position in "numbers" you currently are in when you try "initializing" the array. This is one use case the "advanced" loop isn't made for.

在你的情况下,你不能。For-each 隐藏了底层集合上的迭代器,因此当您尝试“初始化”数组时,您无法确定当前在“数字”中的位置。这是“高级”循环不适用的一种用例。

回答by ryanprayogo

The 'advanced' for-loop doesn't expose the counter to you, and hence, you cannot write the result of counter++to the specific array slot.

“高级”for 循环不会向您公开计数器,因此,您无法将结果写入counter++特定的数组槽。

Your case is the case where the 'advanced' for-loop isn't made for. See:

您的情况是“高级”for 循环不是针对的。看:

http://java.sun.com/j2se/1.5.0/docs/guide/language/foreach.html

http://java.sun.com/j2se/1.5.0/docs/guide/language/foreach.html

Take a look at the last paragraph.

看看最后一段。

回答by matsev

The reason why you get null values as output is that you do not store any values in the array.

获得空值作为输出的原因是您没有在数组中存储任何值。

You can use the foreach loop to initialize the array, but then you must manually maintain a counter to reference the array elements:

您可以使用 foreach 循环来初始化数组,但是您必须手动维护一个计数器来引用数组元素:

for (Integer i : numbers ){
    numbers[counter] = counter;
    counter++;
}

Clearly, this is not the intended use case for the foreach loop. To solve your problem, I would suggest using the "traditional" for loop:

显然,这不是 foreach 循环的预期用例。为了解决您的问题,我建议使用“传统” for 循环:

for (int i = 0; i < numbers.length; i++){
    numbers[i] = i;
}

Note, it is possible to fill all elements with the same value using Arrays.fill(int[] array, int val).

请注意,可以使用Arrays.fill(int[] array, int val)用相同的值填充所有元素。