Java 在 for 循环中, (int i : tall) 做什么,其中高是一个 int 数组

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

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

javaarraysfor-loop

提问by Makri

As the header says, I was tipped by some people that if I wanted to print the sum of everything in an array of numbers, I should use the above-mentioned parameter for a for-loop (code will follow if further explanation is needed). But what is the exact definiton of what that does? The :-part I mean. Is it; for every number i in the array tall?

正如标题所说,有人提示我,如果我想打印数字数组中所有内容的总和,我应该使用上述参数进行 for 循环(如果需要进一步解释,代码将随之而来) . 但是它的确切定义是什么?:-part 我的意思是。是吗; 对于数组中的每个数字 i 高吗?

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+=;
    }
    }
}

采纳答案by Konstantin Yovkov

This is how for-eachloops are represented in Java.

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

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

Read it as: For each integer iin the array called tall...

读作:对于i数组中的每个整数称为tall...

回答by SudoRahul

for(int i : listOfInt)

for(int i : listOfInt)

This is the advanced(enhanced) forloop, also called the for-eachloop, which iterates over each element of the list provided on the right hand side of the :, assigning each value iteratively to the variable on the left of the :.

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

It basically means, for each element in the array/arraylist (in your case, its an array called tail).

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

Have a look at the docs herefor more detailed info.

查看此处文档以获取更详细的信息。

回答by Maroun

It's enhanced loop. It was introduced in Java 5 to simplify looping. You can read it as "For each intintall" and it's like writing:

它是增强的循环。它在 Java 5 中被引入以简化循环。你可以把它读成“ For each intintall”,就像写:

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

Although it's simpler, but it's not flexible as the for loop.. It's good when you don't really care about the index of the elements.

虽然更简单,但不如for循环灵活。。当你不太关心元素的索引时,这很好。

More reading.

更多阅读

回答by Suresh Atta

That enhanced for loop equals to

增强的 for 循环等于

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

The below form

下面的表格

 for(int i : tall){

Is the short hand form the classical for loop.

是经典 for 循环的简写形式。

Note:

笔记:

But there is a condition to use the above form

但是有一个条件可以使用上面的形式

Form Language specification

表单语言规范

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

表达式的类型必须是 Iterable 或数组类型(第 10.1 节),否则会发生编译时错误。

Here the docs from oracle

这里是oracle文档

Finally

最后

 int sum = 0;
    for(int i : tall){
        sum+=;  // sum = sum+i
    }

That means adding the all elements in the array.

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

If it Collection, See how that loop converts :What are for-each expressions in Java translated to?

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

回答by CloudyMarble

Read form the documentation:

阅读文档

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.

for 语句还有另一种形式,专为通过集合和数组进行迭代而设计。这种形式有时被称为增强的 for 语句,可用于使循环更加紧凑和易于阅读。

The following program, EnhancedForDemo, uses the enhanced for to loop through the array:

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

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

回答by Ruchira Gayan Ranaweera

It is a for-each loop

这是一个 for-each 循环

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

Out put will be

输出将是

 1
 3
 5
 7
 8

But this work in JDK 1.7 and higher versions only. Early JDK version support for-each loop with Object type.

但这仅适用于 JDK 1.7 及更高版本。早期的 JDK 版本支持对象类型的 for-each 循环。

And following for loop similar to this for-each

并遵循类似于此 for-each 的 for 循环

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

回答by SSP

“Enhanced” for loops

“增强” for 循环

INTERMEDIATE

中间的

The usual way to step through all the elements of an array in order is with a "standard" for loop, for example,

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

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

The so-called enhanced for loop is a simpler way to do this same thing. (The colon in the syntax can be read as "in.")

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

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

The enhanced for loop was introduced in Java 5 as a simpler way to iterate through all the elements of a Collection (Collections are not covered in these pages). It can also be used for arrays, as in the above example, but this is not the original purpose.

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

Enhanced for loops are simple but inflexible. They can be used when you wish to step through the elements of the array in first-to-last order, and you do not need to know the index of the current element. In all other cases, the "standard" for loop should be preferred.

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

Two additional statement types, break and continue, can also control the behavior of enhanced for loops.

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

Copied from http://www.cis.upenn.edu/~matuszek/General/JavaSyntax/enhanced-for-loops.html

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

And it's a humble request to all that first try to find your answer on google

对于所有第一次尝试在谷歌上找到答案的人来说,这是一个谦虚的请求