Java Iterator与ListIterator的主要区别

来源:互联网 发布:网络兼职招聘网站 编辑:程序博客网 时间:2024/06/06 04:20

>

我们在使用List,Set的时候,为了实现对其数据的遍历,我们经常使用到了
Iterator()(迭代器)。使用迭代器,你不需要干涉其遍历的过程,只需要每次取
出一个你想要的数据进行处理就可以了。
但是在使用的时候也是有不同的。List和Set都可以通过调用iterator()方法来获得其迭代器。对List来说,你也可以通过listIterator()取得其迭代器,两种迭代器在有些时候是不能通用的,Iterator和ListIterator主要区别在以下方面:

  1. ListIterator有add()方法,可以向List中添加对象,而Iterator不能

  2. ListIterator和Iterator都有hasNext()和next()方法,可以实现顺序向后遍历,但是ListIterator()扩展了List和Iterator的功能,具有hasPrevious()和previous()方法,可以实现对表的逆向(或者顺序向前)遍历。但是,Iterator不可以。

  3. ListIterator可以定位当前的索引位置,nextIndex()和previousIndex()可以实现。Iterator没有此功能。

  4. 都可实现删除对象,但是ListIterator可以实现对象的修改,set()方法可以实现。Iierator仅能遍历,不能修改。

因为ListIterator的这些功能,可以实现对LinkedList等List数据结构的操作。其实,数组对象也可以用迭代器来实现。

org.apache.commons.collections.iterators.ArrayIterator就可以实现此功能。一般情况下,我们使用Iterator就可以了,如果你需要进行记录的前后反复检索的话,你就可以使用ListIterator来扩展你的功能,(有点象JDBC中的滚动结果集)。

看看API上怎么说:

java.util
Interface ListIterator

All Superinterfaces:
Iterator

public interface ListIterator
extends Iterator

An iterator for lists that allows the programmer to traverse the list in either direction, modify the list during iteration, and obtain the iterator’s current position in the list. A ListIterator has no current element; its cursor position always lies between the element that would be returned by a call to previous() and the element that would be returned by a call to next(). In a list of length n, there are n+1 valid index values, from 0 to n, inclusive.

      Element(0)   Element(1)   Element(2)   ... Element(n)       ^            ^            ^            ^               ^

Index: 0 1 2 3 n+1

Note that the remove() and set(Object) methods are not defined in terms of the cursor position; they are defined to operate on the last element returned by a call to next() or previous().

This interface is a member of the Java Collections Framework.

Since:
1.2
See Also:
Collection, List, Iterator, Enumeration

Method Summary
void add(Object o)
Inserts the specified element into the list (optional operation).
boolean hasNext()
Returns true if this list iterator has more elements when traversing the list in the forward direction.
boolean hasPrevious()
Returns true if this list iterator has more elements when traversing the list in the reverse direction.
Object next()
Returns the next element in the list.
int nextIndex()
Returns the index of the element that would be returned by a subsequent call to next.
Object previous()
Returns the previous element in the list.
int previousIndex()
Returns the index of the element that would be returned by a subsequent call to previous.
void remove()
Removes from the list the last element that was returned by next or previous (optional operation).
void set(Object o)
Replaces the last element returned by next or previous with the specified element (optional operation).

Method Detail
hasNext

public boolean hasNext()

Returns true if this list iterator has more elements when traversing the list in the forward direction. (In other words, returns true if next would return an element rather than throwing an exception.)Specified by:    hasNext in interface IteratorReturns:    true if the list iterator has more elements whentraversing the list in the forward direction.

next

public Object next()

Returns the next element in the list. This method may be called repeatedly to iterate through the list, or intermixed with calls to previous to go back and forth. (Note that alternating calls to next and previous will return the same element repeatedly.)Specified by:    next in interface IteratorReturns:    the next element in the list.Throws:    NoSuchElementException - if the iteration has no next element.

hasPrevious

public boolean hasPrevious()

Returns true if this list iterator has more elements when traversing the list in the reverse direction. (In other words, returns true if previous would return an element rather than throwing an exception.)Returns:    true if the list iterator has more elements when traversing the list in the reverse direction.

previous

public Object previous()

Returns the previous element in the list. This method may be called repeatedly to iterate through the list backwards, or intermixed with calls to next to go back and forth. (Note that alternating calls to next and previous will return the same element repeatedly.)Returns:    the previous element in the list.Throws:    NoSuchElementException - if the iteration has no previous element.

nextIndex

public int nextIndex()

Returns the index of the element that would be returned by a subsequent call to next. (Returns list size if the list iterator is at the end of the list.)Returns:    the index of the element that would be returned by a subsequent call to next, or list size if list iterator is at end of list.

previousIndex

public int previousIndex()

Returns the index of the element that would be returned by a subsequent call to previous. (Returns -1 if the list iterator is at the beginning of the list.)Returns:    the index of the element that would be returned by a subsequent call to previous, or -1 if list iterator is at beginning of list.

remove

public void remove()

Removes from the list the last element that was returned by next or previous (optional operation). This call can only be made once per call to next or previous. It can be made only if ListIterator.add has not been called after the last call to next or previous.Specified by:    remove in interface IteratorThrows:    UnsupportedOperationException - if the remove operation is not supported by this list iterator.    IllegalStateException - neither next nor previous have been called, or remove or add have been called after the last call to * next or previous.

set

public void set(Object o)

Replaces the last element returned by next or previous with the specified element (optional operation). This call can be made only if neither ListIterator.remove nor ListIterator.add have been called after the last call to next or previous.Parameters:    o - the element with which to replace the last element returned by next or previous.Throws:    UnsupportedOperationException - if the set operation is not supported by this list iterator.    ClassCastException - if the class of the specified element prevents it from being added to this list.    IllegalArgumentException - if some aspect of the specified element prevents it from being added to this list.    IllegalStateException - if neither next nor previous have been called, or remove or add have been called after the last call to next or previous.

add

public void add(Object o)

Inserts the specified element into the list (optional operation). The element is inserted immediately before the next element that would be returned by next, if any, and after the next element that would be returned by previous, if any. (If the list contains no elements, the new element becomes the sole element on the list.) The new element is inserted before the implicit cursor: a subsequent call to next would be unaffected, and a subsequent call to previous would return the new element. (This call increases by one the value that would be returned by a call to nextIndex or previousIndex.)Parameters:    o - the element to insert.Throws:    UnsupportedOperationException - if the add method is not supported by this list iterator.    ClassCastException - if the class of the specified element prevents it from being added to this list.    IllegalArgumentException - if some aspect of this element prevents it from being added to this list

ArrayList和LinkedList的大致区别

1.ArrayList是实现了基于动态数组的数据结构,LinkedList基于链表的数据结构。

2.对于随机访问get和set,ArrayList觉得优于LinkedList,因为LinkedList要移动指针。
3.对于新增和删除操作add和remove,LinedList比较占优势,因为ArrayList要移动数据

0 0