Iterator和Listiterator之间的区别?

Iterator和Listiterator之间的区别?

问题描述:

Iterator ite = Set.iterator();
Iterator ite = List.iterator();

ListIterator listite = List.listIterator();

我们可以使用 Iterator 来遍历设置或列表地图。但是 ListIterator 只能用于遍历 List ,它不能遍历 Set 。为什么?

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?

我知道主要的不同之处在于,使用迭代器,我们只能在一个方向上旅行但是使用 ListIterator 我们可以双向旅行。还有其他差异吗? ListIterator 优于 Iterator

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?

差异列在Javadoc中 ListIterator

The differences are listed in the Javadoc for ListIterator

您可以


  • 向后迭代

  • 随时获取迭代器。

  • 随时添加新值。

  • 设置a此时的新价值。

  • iterate backwards
  • obtain the iterator at any point.
  • add a new value at any point.
  • set a new value at that point.