Most visited

Recently visited

Added in API level 1

ListIterator

public interface ListIterator
implements Iterator<E>

java.util.ListIterator<E>


列表的迭代器,允许程序员在任一方向遍历列表,在迭代期间修改列表,并获取列表中迭代器的当前位置。 A ListIterator没有当前元素; 它的光标位置始终位于通过调用previous()返回的元素和通过调用next()返回的元素next() 长度为n的迭代器具有n+1可能的光标位置,如下面的^^ )所示:

                      Element(0)   Element(1)   Element(2)   ... Element(n-1)
 cursor positions:  ^            ^            ^            ^                  ^
 
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().

该界面是 Java Collections Framework的成员。

也可以看看:

Summary

Public methods

abstract void add(E e)

将指定的元素插入列表(可选操作)。

abstract boolean hasNext()

如果此列表迭代器在向前方向遍历列表时有更多元素,则返回 true

abstract boolean hasPrevious()

如果此列表迭代器在反向方向上遍历列表时返回 true

abstract E next()

返回列表中的下一个元素并前进光标位置。

abstract int nextIndex()

返回随后调用 next()将返回的元素的索引。

abstract E previous()

返回列表中的前一个元素并向后移动光标位置。

abstract int previousIndex()

返回随后调用 previous()将返回的元素的索引。

abstract void remove()

从列表中删除由 next()previous() (可选操作)返回的最后一个元素。

abstract void set(E e)

用指定元素替换由 next()previous()返回的最后一个元素(可选操作)。

Inherited methods

From interface java.util.Iterator

Public methods

add

Added in API level 1
void add (E e)

将指定的元素插入列表(可选操作)。 该元素紧接在由next()返回的元素之前(如果有的话)以及在由previous()返回的元素之后(如果有的话)。 (如果列表不包含元素,则新元素成为列表中的唯一元素。)新元素插入到隐式游标之前:对next的后续调用将不受影响,并且随后调用previous将返回新元素。 (此呼叫会增加1个呼叫的返回值nextIndexpreviousIndex

Parameters
e E: 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

hasNext

Added in API level 1
boolean hasNext ()

如果此列表迭代器在向前方向遍历列表时有更多元素,则返回true (换句话说,如果next()将返回一个元素而不是引发异常,则返回true

Returns
boolean true if the list iterator has more elements when traversing the list in the forward direction

hasPrevious

Added in API level 1
boolean hasPrevious ()

如果此列表迭代器在反向方向上遍历列表时返回true (换句话说,如果previous()将返回一个元素而不是引发异常,则返回true

Returns
boolean true if the list iterator has more elements when traversing the list in the reverse direction

next

Added in API level 1
E next ()

返回列表中的下一个元素并前进光标位置。 可以重复调用此方法遍历列表,或者与调用previous()混合来回。 (请注意,对nextprevious交替呼叫将重复返回相同的元素。)

Returns
E the next element in the list
Throws
NoSuchElementException if the iteration has no next element

nextIndex

Added in API level 1
int nextIndex ()

返回随后调用next()将返回的元素的索引。 (如果列表迭代器位于列表的末尾,则返回列表大小。)

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

previous

Added in API level 1
E previous ()

返回列表中的前一个元素并向后移动光标位置。 可以重复调用此方法以向后遍历列表,或与调用next()来回混合。 (请注意,对nextprevious交替呼叫将重复返回相同的元素。)

Returns
E the previous element in the list
Throws
NoSuchElementException if the iteration has no previous element

previousIndex

Added in API level 1
int previousIndex ()

返回后续调用previous()将返回的元素的索引。 (如果列表迭代器位于列表的开头,则返回-1。)

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

remove

Added in API level 1
void remove ()

从列表中删除由next()previous() (可选操作)返回的最后一个元素。 此呼叫只能在每次致电nextprevious进行一次。 它可制成只有add(E)尚未到最后通话后称为next或者previous

Throws
UnsupportedOperationException if the remove operation is not supported by this list iterator
IllegalStateException if neither next nor previous have been called, or remove or add have been called after the last call to next or previous

set

Added in API level 1
void set (E e)

用指定元素(可选操作)替换next()previous()返回的最后一个元素。 只有在最后一次致电nextpreviousremove()add(E)remove()呼叫时才能进行此呼叫。

Parameters
e E: 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

Hooray!