Iterate with for loop or while loop?

后端 未结 15 1487
野趣味
野趣味 2020-12-01 13:31

I often see code like:

Iterator i = list.iterator();
while(i.hasNext()) {
    ...
}

but I write that (when Java 1.5 isn\'t available or for

15条回答
  •  清歌不尽
    2020-12-01 14:13

    if you're only going to use the iterator once and throw it away, the second form is preferred; otherwise you must use the first form

提交回复
热议问题