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

前端 未结 4 1365
走了就别回头了
走了就别回头了 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:46

    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

提交回复
热议问题