每个循环如何防止空列表? [英] How does a for each loop guard against an empty list?
问题描述
我阅读了 http://www.leepoint.net/notes-java/flow/循环/ foreach.html 。对于(int i = 0; i< arr.length; i ++)(
)的每个等价物,
类型var = arr [i];
body-of-loop
}
是
for(type var:arr){
body-of-loop
}
我的问题是每个循环如何为空列表工作。我知道常规的循环,arr.length将只计算为0,循环不会执行。怎么样为每个循环?
我的问题是如何为每个循环工作for a empty list
ForEach
也以同样的方式工作。如果长度为零,那么循环永远不会执行。
它们之间的唯一区别是当你使用 ForEach
循环想要迭代列表或数组的所有项目,而对于循环,如果正常,则可以控制开始和结束索引。
I read on http://www.leepoint.net/notes-java/flow/loops/foreach.html. the for each equivalent to
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 for the for each loop?
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.
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.
这篇关于每个循环如何防止空列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!