How does a for each loop guard against an empty list?

前端 未结 4 1368
走了就别回头了
走了就别回头了 2021-02-05 02:17

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[         


        
4条回答
  •  说谎
    说谎 (楼主)
    2021-02-05 02:23

    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.

提交回复
热议问题