关于java:在for循环中,(int i:tall)做什么,其中tall是int数组

in a for-loop, what does the (int i : tall) do, where tall is an array of int

本问题已经有最佳答案,请猛点这里访问。

正如标题所说,有些人向我倾斜,如果我想在数组数组中打印所有内容的总和,我应该使用上述参数进行for循环(如果需要进一步说明,代码将会跟随)。 但具体的确切定义是什么呢? : - 我的意思是。 是吗; 对于阵列中的每个数字我都很高?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import java.util.*;

class Uke36{
    public static void main(String[]args){

    Scanner input=new Scanner(System.in);
    int[] tall=new int[5];

    for (int i=0; i<=4; i++){
        System.out.println("Vennligst oppgi det" + (i+1) +". tallet:");
        tall[i]=input.nextInt();
    }
    int sum = 0;
    for(int i : tall){
        sum+=;
    }
    }
}


这就是用Java表示for-each循环的方式。

1
2
3
for(int i : tall){
    sum+=;
}

基本上,这里说:

1
For each integer i in the array called **tall** ...


增强的循环等于

1
2
3
for (int i=0; i < tall.length; i++) {
    System.out.println("Element:" + tall[i]);
}

以下表格

1
 for(int i : tall){

短手是否形成经典的for循环。

注意:

但是有条件使用上述形式

表单语言规范

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

这里是来自oracle的文档

最后

1
2
3
4
 int sum = 0;
    for(int i : tall){
        sum+=;  // sum = sum+i
    }

这意味着添加数组中的所有元素。

如果是Collection,请查看该循环如何转换:什么是Java中的每个表达式转换为?


它是增强的循环。它是在Java 5中引入的,以简化循环。您可以将其读作"对于tall中的每个int",这就像写作:

1
2
for(int i = 0; i < tall.length; i++)
   ...

虽然它更简单,但它不像for循环那样灵活。当你真的不关心元素的索引时这很好。

更多阅读。


循环"增强"

中间

顺序遍历数组中所有元素的常用方法是使用"标准"for循环,例如,

1
2
3
for (int i = 0; i < myArray.length; i++) {
    System.out.println(myArray[i]);
}

所谓的增强for循环是一种更简单的方法来做同样的事情。 (语法中的冒号可以读作"in。")

1
2
3
for (int myValue : myArray) {
    System.out.println(myValue);
}

在Java 5中引入了增强的for循环作为迭代Collection的所有元素的简单方法(这些页面中未涵盖集合)。它也可以用于数组,如上例所示,但这不是最初的目的。

增强的for循环很简单但不灵活。当您希望以倒数第一顺序遍历数组的元素时,可以使用它们,并且您不需要知道当前元素的索引。在所有其他情况下,循环的"标准"应该是首选。

另外两种语句类型break和continue也可以控制增强的for循环的行为。

复制自http://www.cis.upenn.edu/~matuszek/General/JavaSyntax/enhanced-for-loops.html

对于所有首先尝试在谷歌上找到答案的人来说,这是一个不起眼的要求


for(int i : listOfInt)

这是高级(增强型)for循环,也称为for-each循环,它迭代:右侧提供的列表中的每个元素,将每个值迭代地分配给左侧的变量的:

它基本上意味着,对于数组/ arraylist中的每个元素(在您的情况下,它是一个名为tail的数组)。

有关详细信息,请查看此处的文档。


这是一个for-each循环

1
2
3
4
   int[] arr=new int[] {1,3,5,7,8};
   for(int i:arr){
       System.out.println(i);
   }

出局将是

1
2
3
4
5
 1
 3
 5
 7
 8

但这仅适用于JDK 1.7及更高版本。早期的JDK版本支持 - 具有Object类型的每个循环。

跟随for循环类似于for-each

1
2
3
4
    int[] arr=new int[] {1,3,5,7,8};
    for(int i=0;i<arr.length;i++){
        System.out.println(arr[i]);
    }


阅读文档:

The for statement also has another form designed for iteration through
Collections and arrays This form is sometimes referred to as the
enhanced for statement, and can be used to make your loops more
compact and easy to read.

以下程序EnhancedForDemo使用增强的for循环
通过数组:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class EnhancedForDemo {
        public static void main(String[] args){
             int[] numbers =
                 {1,2,3,4,5,6,7,8,9,10};
             for (int item : numbers) {
                 System.out.println("Count is:" + item);
             }
        }
    }

In this example, the variable item holds the current value from the numbers array. The output from this program is the same as before:

Count is: 1
Count is: 2
Count is: 3
Count is: 4
Count is: 5
Count is: 6
Count is: 7
Count is: 8
Count is: 9
Count is: 10