[英]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
来遍历Set
或List
或Map
。 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
? ListIterator
比Iterator
什么优势?
The differences are listed in the Javadoc for ListIterator差异在ListIterator的 Javadoc 中列出
You can你可以
There are two differences:有两个区别:
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:迭代器中的方法:
hasNext()
next()
remove()
Iterator iterator = Set.iterator(); Iterator iterator = List.iterator();
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 中的方法:
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:以下是它们之间的区别:
iterator
you can move only forward, but with ListIterator
you can move backword also while reading the elements.iterator
您只能向前移动,但使用ListIterator
您也可以在读取元素时向后移动。ListIterator
you can obtain the index at any point while traversing, which is not possible with iterator
s.ListIterator
您可以在遍历时随时获取索引,而使用iterator
则无法实现。iterator
you can check only for next element available or not, but in listiterator
you can check previous and next elements.iterator
您只能检查下一个元素是否可用,但在listiterator
您可以检查上一个和下一个元素。listiterator
you can add new element at any point of time, while traversing.listiterator
您可以在遍历的任何时间点添加新元素。 Not possible with iterator
.iterator
不可能。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.