考虑:

List<String> someList = new ArrayList<String>();
// add "monkey", "donkey", "skeleton key" to someList
for (String item : someList) {
    System.out.println(item);
}

如果不使用for each语法,等价的for循环会是什么样子?


当前回答

正如许多好答案所说,如果一个对象想要使用for each循环,它必须实现Iterable接口。

我将发布一个简单的示例,并尝试以不同的方式解释for each循环的工作原理。

对于每个循环示例:

public class ForEachTest {

    public static void main(String[] args) {

        List<String> list = new ArrayList<String>();
        list.add("111");
        list.add("222");

        for (String str : list) {
            System.out.println(str);
        }
    }
}

然后,如果我们使用javap来反编译这个类,我们将得到这个字节码示例:

public static void main(java.lang.String[]);
    flags: ACC_PUBLIC, ACC_STATIC
    Code:
      stack=2, locals=4, args_size=1
         0: new           #16                 // class java/util/ArrayList
         3: dup
         4: invokespecial #18                 // Method java/util/ArrayList."<init>":()V
         7: astore_1
         8: aload_1
         9: ldc           #19                 // String 111
        11: invokeinterface #21,  2           // InterfaceMethod java/util/List.add:(Ljava/lang/Object;)Z
        16: pop
        17: aload_1
        18: ldc           #27                 // String 222
        20: invokeinterface #21,  2           // InterfaceMethod java/util/List.add:(Ljava/lang/Object;)Z
        25: pop
        26: aload_1
        27: invokeinterface #29,  1           // InterfaceMethod java/util/List.iterator:()Ljava/util/Iterator;

从示例的最后一行可以看到,编译器将在编译时自动将for每个关键字的使用转换为Iterator的使用。这可以解释为什么对象(它没有实现Iterable接口)在尝试使用for each循环时会抛出异常。

其他回答

在Java 8功能中,您可以使用以下功能:

List<String> messages = Arrays.asList("First", "Second", "Third");

void forTest(){
    messages.forEach(System.out::println);
}

输出

First
Second
Third

这里有一个答案,它不假定了解Java迭代器。它不太精确,但对教育有用。

在编程时,我们通常编写如下代码:

char[] grades = ....
for(int i = 0; i < grades.length; i++) {   // for i goes from 0 to grades.length
    System.out.print(grades[i]);           // Print grades[i]
}

foreach语法允许以更自然、更少语法噪音的方式编写这种常见模式。

for(char grade : grades) {   // foreach grade in grades
    System.out.print(grade); // print that grade
}

此外,此语法对于不支持数组索引但实现Java Iterable接口的Lists或Set等对象有效。

在Java8中,他们引入了forEach。使用它列表,可以循环地图。

使用for each循环列表

List<String> someList = new ArrayList<String>();
someList.add("A");
someList.add("B");
someList.add("C");

someList.forEach(listItem -> System.out.println(listItem))

or

someList.forEach(listItem-> {
     System.out.println(listItem); 
});

使用for each循环映射

Map<String, String> mapList = new HashMap<>();
    mapList.put("Key1", "Value1");
    mapList.put("Key2", "Value2");
    mapList.put("Key3", "Value3");

mapList.forEach((key,value)->System.out.println("Key: " + key + " Value : " + value));

or

mapList.forEach((key,value)->{
    System.out.println("Key : " + key + " Value : " + value);
});

代码应为:

import java.util.ArrayList;
import java.util.List;

public class ForLoopDemo {

  public static void main(String[] args) {

    List<String> someList = new ArrayList<String>();

    someList.add("monkey");
    someList.add("donkey");
    someList.add("skeleton key");

    // Iteration using For Each loop
    System.out.println("Iteration using a For Each loop:");
    for (String item : someList) {
      System.out.println(item);
    }

    // Iteration using a normal For loop
    System.out.println("\nIteration using normal For loop: ");
    for (int index = 0; index < someList.size(); index++) {
      System.out.println(someList.get(index));
    }
  }
}

每个的构造也对数组有效。例如

String[] fruits = new String[] { "Orange", "Apple", "Pear", "Strawberry" };

for (String fruit : fruits) {
    // fruit is an element of the `fruits` array.
}

这基本上等同于

for (int i = 0; i < fruits.length; i++) {
    String fruit = fruits[i];
    // fruit is an element of the `fruits` array.
}

因此,总体总结如下:[nsayer]以下是正在发生的事情的较长形式:

for(Iterator<String>i=someList.iiterator();i.hasNext();){字符串项=i.next();System.out.println(项);}注意,如果您需要使用i.移除();或访问实际的迭代器不能使用for(:)习惯用法,因为实际的迭代器只是推断。

[丹尼斯·布埃诺]

nsayer的回答暗示了这一点,但是值得注意的是,OP用于(..)当“someList”为任何实现的东西java.lang.Iterable--它没有成为列表,或来自甚至你自己的类型,因此,可以与此一起使用语法。