考虑:

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

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


当前回答

foreach循环语法为:

for (type obj:array) {...}

例子:

String[] s = {"Java", "Coffe", "Is", "Cool"};
for (String str:s /*s is the array*/) {
    System.out.println(str);
}

输出:

Java
Coffe
Is
Cool

警告:可以使用foreach循环访问数组元素,但不能初始化它们。为此使用原始for循环。

警告:必须将数组的类型与其他对象匹配。

for (double b:s) // Invalid-double is not String

如果要编辑元素,请使用原始for循环,如下所示:

for (int i = 0; i < s.length-1 /*-1 because of the 0 index */; i++) {
    if (i==1) //1 because once again I say the 0 index
        s[i]="2 is cool";
    else
        s[i] = "hello";
}

现在,如果我们将数据转储到控制台,我们会得到:

hello
2 is cool
hello
hello

其他回答

它看起来像这样。非常粗糙。

for (Iterator<String> i = someList.iterator(); i.hasNext(); )
        System.out.println(i.next());

Sun文档中对每一项都有很好的描述。

如JLS中所定义的,每个循环的a可以有两种形式:

如果表达式类型是Iterable的子类型,则转换为:List<String>someList=newArrayList<String>();someList.add(“苹果”);someList.add(“球”);for(字符串项:someList){System.out.println(项);}//翻译为:for(Iterator<String>stringIterator=someList.iiterator();stringIterator.hasNext();){字符串项=stringIterator.next();System.out.println(项);}如果表达式必须具有数组类型T[],则:String[]someArray=新字符串[2];someArray[0]=“苹果”;someArray[1]=“球”;for(字符串项2:someArray){系统输出打印(第2项);}//翻译为:for(int i=0;i<someArray.length;i++){字符串item2=someArray[i];系统输出打印(第2项);}

Java8已经引入了一些流,这些流在适当大小的数据集中通常表现得更好。我们可以将它们用作:

someList.stream().forEach(System.out::println);
Arrays.stream(someArray).forEach(System.out::println);

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

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

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

输出

First
Second
Third

foreach循环语法为:

for (type obj:array) {...}

例子:

String[] s = {"Java", "Coffe", "Is", "Cool"};
for (String str:s /*s is the array*/) {
    System.out.println(str);
}

输出:

Java
Coffe
Is
Cool

警告:可以使用foreach循环访问数组元素,但不能初始化它们。为此使用原始for循环。

警告:必须将数组的类型与其他对象匹配。

for (double b:s) // Invalid-double is not String

如果要编辑元素,请使用原始for循环,如下所示:

for (int i = 0; i < s.length-1 /*-1 because of the 0 index */; i++) {
    if (i==1) //1 because once again I say the 0 index
        s[i]="2 is cool";
    else
        s[i] = "hello";
}

现在,如果我们将数据转储到控制台,我们会得到:

hello
2 is cool
hello
hello

代码应为:

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));
    }
  }
}