For-each vs Iterator. Which will be the better option

后端 未结 8 1338
悲&欢浪女
悲&欢浪女 2020-12-02 09:14

Consider the following scenario.

List list = new ArrayList<>();

Now I added the String values for this li

8条回答
  •  南笙
    南笙 (楼主)
    2020-12-02 09:42

    for-each is an advanced looping construct. Internally it creates an Iterator and iterates over the Collection. Only possible advantage of using an actual Iterator object over the for-each construct is that you can modify your collection using Iterator's methods like .remove(). Modifying the collection without using Iterator's methods while iterating will produce a ConcurrentModificationException.

提交回复
热议问题