Java 中的每个循环的原始数组如何与 new 一起使用?

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

How does primitive array work with new for each loop in Java?

javaarraysforeach

提问by AS_

I understand that new for each loop works with Iterable and arrays, but I don't know what goes behind the scenes when working with arrays.

我知道 new for 每个循环适用于 Iterable 和数组,但我不知道使用数组时的幕后情况。

Can anyone help me understand this? Thanks in advance.

任何人都可以帮助我理解这一点吗?提前致谢。

int[] number = new int[10];

for(int i: number) {

}

回答by Patricia Shanahan

The loop is equivalent to:

该循环等效于:

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

where j is an internally generated reference that does not conflict with normal user identifiers.

其中 j 是内部生成的引用,不与普通用户标识符冲突。

回答by DwB

A bit late, but here it is.

有点晚了,但它来了。

The compiler knows if you are using the for-each loop statement for a collection or for an array.

编译器知道您是将 for-each 循环语句用于集合还是数组。

If used for collection, the compiler translates the for-each loop to the equivalent for loop using an Iterator.

如果用于收集,编译器将使用Iterator.

If used for an array, the compiler translates the for-each loop to the equivalent for loop using an index variable.

如果用于数组,编译器会使用索引变量将 for-each 循环转换为等效的 for 循环。

Here is a description at oracle.com

这是 oracle.com 上的说明

回答by Edwin Dalorzo

In your code, you allocate an array of 10 integers in the memory and obtain a reference to it. In the for-loop you simply iterate over every item in the array, which initially will be 0 for all the items. The value of every item will be stored in the variable ideclared in your for-loop as you iterate the array elements.

在您的代码中,您在内存中分配了一个包含 10 个整数的数组并获得了对它的引用。在 for 循环中,您只需迭代数组中的每个项目,所有项目的初始值都是 0。i当您迭代数组元素时,每个项目的值都将存储在for 循环中声明的变量中。

回答by GreyBeardedGeek

this is equivalent to:

这相当于:

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

回答by AlexWien

This is the equivalent to:

这相当于:

final int len = number.length;
for(int j = 0; j < len; j++) {
  int i = number[j];
}

Note that the forEach will not evaluate the .length in each loop. This might be also be eliminated by the JVM, but especially in case of collections, where some would use

请注意, forEach 不会评估每个循环中的 .length 。这也可能被 JVM 消除,但特别是在集合的情况下,有些人会使用

for(int j = 0; j < collection.size(); j++) {

it makes a (small) difference to the faster

它对更快的产生(小)差异

int len = collection.size()
for(int j = 0; j < len; j++) {

回答by KayV

IntStream.range(1,4)can be used, if using java 8.

IntStream.range(1,4)可以使用,如果使用 java 8。

回答by Woot4Moo

The for each over arrays is essentially "sugar" over this construct:

for each over 数组本质上是这个构造上的“糖”:

for(int i = 0;i<number.length;i++)
{  
}

I would imagine this was provided as a construct of the language so that people could use the enhanced for loop over a structure that was iterated over in the old way.

我想这是作为语言的一种构造提供的,以便人们可以在以旧方式迭代的结构上使用增强的 for 循环。