考虑:
List<String> someList = new ArrayList<String>();
// add "monkey", "donkey", "skeleton key" to someList
for (String item : someList) {
System.out.println(item);
}
如果不使用for each语法,等价的for循环会是什么样子?
考虑:
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循环更容易阅读,并且不需要单独的迭代器,但只有在只读迭代过程中才能真正使用。
其他回答
还要注意,在原始问题中使用“foreach”方法确实有一些限制,例如在迭代过程中无法从列表中删除项目。
新的for循环更容易阅读,并且不需要单独的迭代器,但只有在只读迭代过程中才能真正使用。
这是一个等价的表达式。
for(Iterator<String> sit = someList.iterator(); sit.hasNext(); ) {
System.out.println(sit.next());
}
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 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循环时会抛出异常。
维基百科中提到的foreach循环的概念如下:
然而,与其他for循环构造不同,foreach循环通常保持没有明确的反击:他们基本上说“这样做而不是“做x次”。这样可以避免潜在的一个错误,使代码更容易阅读。
因此,foreach循环的概念描述了该循环不使用任何显式计数器,这意味着不需要使用索引在列表中遍历,因此它将用户从一个错误中解脱出来。为了描述这一错误的一般概念,让我们举一个使用索引在列表中遍历的循环的例子。
// In this loop it is assumed that the list starts with index 0
for(int i=0; i<list.length; i++){
}
但是假设列表以索引1开始,那么这个循环将抛出一个异常,因为它将在索引0处找不到元素,这个错误被称为off-by-one错误。因此,为了避免这一错误,使用了foreach循环的概念。可能还有其他优点,但这就是我认为使用foreach循环的主要概念和优点。