简体   繁体   English

迭代器和列表迭代器的区别?

[英]Difference between Iterator and Listiterator?

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 .我们可以使用Iterator来遍历SetListMap But ListIterator can only be used to traverse a List , it can't traverse a Set .但是ListIterator只能用于遍历List ,不能遍历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.我知道主要的区别在于,使用迭代器我们只能在一个方向上行驶,而使用ListIterator我们可以在两个方向上行驶。 Are there any other differences?还有其他区别吗? And any advantages of ListIterator over Iterator ? ListIteratorIterator什么优势?

The differences are listed in the Javadoc for ListIterator差异在ListIterator的 Javadoc 中列出

You can你可以

  • iterate backwards向后迭代
  • obtain the iterator at any point.在任何时候获取迭代器。
  • add a new value at any point.在任何时候添加一个新值。
  • set a new value at that point.在该点设置一个新值。

There are two differences:有两个区别:

  1. We can use Iterator to traverse Set and List and also Map type of Objects.我们可以使用 Iterator 来遍历 Set 和 List 以及 Map 类型的对象。 While a ListIterator can be used to traverse for List-type Objects, but not for Set-type of Objects.虽然 ListIterator 可用于遍历 List 类型的对象,但不适用于 Set 类型的对象。

    That is, we can get a Iterator object by using Set and List, see here:也就是说,我们可以通过使用 Set 和 List 来获取一个 Iterator 对象,参见这里:

    By using Iterator we can retrieve the elements from Collection Object in forward direction only.通过使用迭代器,我们可以仅向前检索集合对象中的元素。

    Methods in Iterator:迭代器中的方法:

    1. hasNext()
    2. next()
    3. remove()
     Iterator iterator = Set.iterator(); Iterator iterator = List.iterator();
  2. But we get ListIterator object only from the List interface, see here:但是我们只能从 List 接口获取 ListIterator 对象,请看这里:

    where as a ListIterator allows you to traverse in either directions (Both forward and backward).其中 ListIterator 允许您在任一方向(向前和向后)遍历。 So it has two more methods like hasPrevious() and previous() other than those of Iterator.因此,除了 Iterator 的方法hasPrevious()它还有两个方法,如hasPrevious()previous() Also, we can get indexes of the next or previous elements (using nextIndex() and previousIndex() respectively )此外,我们可以获取下一个或上一个元素的索引(分别使用nextIndex()previousIndex()

    Methods in ListIterator: ListIterator 中的方法:

    1. hasNext()有下一个()
    2. next()下一个()
    3. previous()以前的()
    4. hasPrevious()有上一个()
    5. remove()消除()
    6. nextIndex()下一个索引()
    7. previousIndex()以前的索引()
     ListIterator listiterator = List.listIterator();

    ie, we can't get ListIterator object from Set interface.即,我们无法从 Set 接口获取 ListIterator 对象。

Reference : - What is the difference between Iterator and ListIterator ?参考: - Iterator 和 ListIterator 之间有什么区别?

Iterator is super class of ListIterator. Iterator 是 ListIterator 的超类。

Here are the differences between them:以下是它们之间的区别:

  1. With iterator you can move only forward, but with ListIterator you can move backword also while reading the elements.使用iterator您只能向前移动,但使用ListIterator您也可以在读取元素时向后移动。
  2. With ListIterator you can obtain the index at any point while traversing, which is not possible with iterator s.使用ListIterator您可以在遍历时随时获取索引,而使用iterator则无法实现。
  3. With iterator you can check only for next element available or not, but in listiterator you can check previous and next elements.使用iterator您只能检查下一个元素是否可用,但在listiterator您可以检查上一个和下一个元素。
  4. With listiterator you can add new element at any point of time, while traversing.使用listiterator您可以在遍历的任何时间点添加新元素。 Not possible with iterator .iterator不可能。
  5. With listiterator you can modify an element while traversing, which is not possible with iterator .使用listiterator您可以在遍历时修改元素,而iterator则无法做到这一点。

Iterator look and feel:迭代器外观:

 public interface Iterator<E> {
    boolean hasNext();
    E next();
    void remove(); //optional-->use only once with next(), 
                         dont use it when u use for:each
    }

ListIterator look and feel: ListIterator 外观和感觉:

public interface ListIterator<E> extends Iterator<E> {
    boolean hasNext();
    E next();
    boolean hasPrevious();
    E previous();
    int nextIndex();
    int previousIndex();
    void remove(); //optional
    void set(E e); //optional
    void add(E e); //optional
}

ListIterator优于Iterator优点是我们可以使用ListIterator 遍历列表的任何时间添加元素

the following is that the difference between iterator and listIterator下面是iterator和listIterator的区别

iterator :迭代器:

boolean hasNext();
E next();
void remove();

listIterator:列表迭代器:

boolean hasNext();
E next();
boolean hasPrevious();
E previous();
int nextIndex();
int previousIndex();
void remove();
void set(E e);
void add(E e);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM