listiterator

Difference between Iterator and Listiterator?

帅比萌擦擦* 提交于 2019-11-26 21:11:29
Iterator ite = Set.iterator(); Iterator ite = List.iterator(); ListIterator listite = List.listIterator(); We can use Iterator to traverse a Set or a List or a Map . But ListIterator can only be used to traverse a List , it can't traverse a Set . Why? I know that the main difference is that with iterator we can travel in only one direction but with ListIterator we can travel both directions. Are there any other differences? And any advantages of ListIterator over Iterator ? The differences are listed in the Javadoc for ListIterator You can iterate backwards obtain the iterator at any point.

Add elements to a List while iterating over it. (Java) [duplicate]

丶灬走出姿态 提交于 2019-11-26 12:45:20
问题 Possible Duplicate: Java: adding elements to a collection during iteration My problem is that I want to expand a list with new elements while iterating over it and I want the iterator to continue with the elements that I just added. From my understanding the ListIterator.add() adds an element before the current element in the list, not after it. Is it possible to achieve this in some other way? 回答1: You can't modify a Collection while iterating over it using an Iterator , except for Iterator

Difference between Iterator and Listiterator?

房东的猫 提交于 2019-11-26 06:10:58
问题 Iterator ite = Set.iterator(); Iterator ite = List.iterator(); ListIterator listite = List.listIterator(); We can use Iterator to traverse a Set or a List or a Map . But ListIterator can only be used to traverse a List , it can\'t traverse a Set . Why? I know that the main difference is that with iterator we can travel in only one direction but with ListIterator we can travel both directions. Are there any other differences? And any advantages of ListIterator over Iterator ? 回答1: The