Java for each 循环如何防止空列表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24845281/
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
How does a for each loop guard against an empty list?
提问by committedandroider
I read on http://www.leepoint.net/notes-java/flow/loops/foreach.html. the for each equivalent to
我在http://www.leepoint.net/notes-java/flow/loops/foreach.html 上阅读。对于每个等价于
for (int i = 0; i < arr.length; i++) {
type var = arr[i];
body-of-loop
}
is
是
for (type var : arr) {
body-of-loop
}
My question is how does a for each loop work for an empty list. I know for the regular for loop, the arr.length will just evaluate to 0 and the loop wont execute. What about the for each loop?
我的问题是 for each 循环如何为空列表工作。我知道对于常规 for 循环, arr.length 只会评估为 0 并且循环不会执行。对于每个循环呢?
采纳答案by Braj
My question is how does a for each loop work for an empty list
我的问题是每个循环如何为空列表工作
ForEach
also works in the same way. If the length is zero then loop is never executed.
ForEach
也以同样的方式工作。如果长度为零,则永远不会执行循环。
The only difference between them is use ForEach
loop when you want to iterate all the items of the list or array whereas in case of normal for
loop you can control start and end index.
它们之间的唯一区别是ForEach
当您想要迭代列表或数组的所有项目时使用循环,而在普通for
循环的情况下,您可以控制开始和结束索引。
回答by libik
Yes, it is equivalent.
是的,它是等价的。
If the list is empty, the for-each cycle is not executed even once.
如果列表为空,则 for-each 循环甚至不会执行一次。
回答by user3810043
It uses the iterator of the Iterable collection, e.g. List. It is the duty of the implementer of the Iterator to write the hasnext() method to return false if there is no next item which will be the case if the collection is empty
它使用 Iterable 集合的迭代器,例如 List。Iterator 的实现者有责任编写 hasnext() 方法,如果没有下一项,则返回 false,如果集合为空,就会出现这种情况
回答by Will Cain
As @user3810043 alludes to in their answer comments, the enhanced for
statement is literally evaluated the same as an equivalent basic for
statement:
正如@user3810043 在他们的回答评论中提到的那样,增强for
语句的实际评估与等效的基本for
语句相同:
14.14.2. The enhanced for statement
...
The type of the Expression must be a subtype of the raw type Iterable or an array type (§10.1), or a compile-time error occurs.
...
Otherwise, the Expression necessarily has an array type, T[].
Let L1 ... Lm be the (possibly empty) sequence of labels immediately preceding the enhanced for statement.
The enhanced for statement is equivalent to a basic for statement of the form:
T[] #a = Expression; L1: L2: ... Lm: for (int #i = 0; #i < #a.length; #i++) { {VariableModifier} TargetType Identifier = #a[#i]; Statement }
#a and #i are automatically generated identifiers that are distinct from any other identifiers (automatically generated or otherwise) that are in scope at the point where the enhanced for statement occurs.
14.14.2. 增强的 for 语句
...
Expression 的类型必须是原始类型 Iterable 的子类型或数组类型(第 10.1 节),否则会发生编译时错误。
...
否则,表达式必须具有数组类型 T[]。
令 L1 ... Lm 是紧接在增强的 for 语句之前的标签序列(可能为空)。
增强的 for 语句等效于以下形式的基本 for 语句:
T[] #a = Expression; L1: L2: ... Lm: for (int #i = 0; #i < #a.length; #i++) { {VariableModifier} TargetType Identifier = #a[#i]; Statement }
#a 和 #i 是自动生成的标识符,它们不同于在增强的 for 语句发生的点范围内的任何其他标识符(自动生成或以其他方式生成)。
^ Quote from The Java? Language Specification
^ 引自The Java?语言规范