版本
JDK8(JDK1.8)
ListIterator接口源码重点
1.ListIterator接口继承Iterator接口,是Iterator接口的扩展版,ListIterator允许沿着两个方向遍历列表(向后next()和向前previous()),同时比Iterator,多了set(.),add(.)方法用于在光标处替换和添加元素
Iterator源码可以看我这篇文章 Iterator
2.ListIterator没有当前元素,只有一个光标位置,其光标位置始终位于调用previous()返回的元素和调用next()返回的元素之间
3.ListIterator接口方法
方法名 | 作用 |
---|---|
boolean hasNext() | 如果此列表迭代器向后遍历列表时还有剩余元素,则返回true |
E next() | 返回列表中的下一个元素 |
boolean hasPrevious(); | 如果此列表迭代器向前遍历列表时还有剩余元素,则返回true |
E previous() | 返回列表中的上一个元素 |
int nextIndex() | 返回的是下一次调用的next返回的元素的索引 |
int previousIndex() | 返回的是下一次调用的previous返回的元素的索引 |
void remove() | 从列表中删除next或previous返回的最后一个元素 |
void set(E e) | 将next或previous返回的最后一个元素替换为指定的元素 |
void add(E e) | 将指定的元素插入列表 |
注意:
- 交替调用next()和previous()方法将重复返回相同的元素
- remove()和set()方法必须在调用next或previous后,且remove或add或set均未被调用时,才能调用,即remove()和set()方法无法在一次next或previous后多次调用,且之间会相互影响,一个方法调用了另一个方法不能调用
- add(.)对next()和previous()方法影响不同,调用add(.)后对next的后续调用将不受影响,对previous的后续调用将返回新元素,
- add(.)会使得下次调用的nextIndex()或previousIndex()两者返回值都增加1
ListIterator接口源码
package java.util;
/**
* 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 {@code ListIterator}
* has no current element; its <I>cursor position</I> always
* lies between the element that would be returned by a call
* to {@code previous()} and the element that would be
* returned by a call to {@code next()}.
* An iterator for a list of length {@code n} has {@code n+1} possible
* cursor positions, as illustrated by the carets ({@code ^}) below:
* 列表的迭代器,允许程序员沿任意方向遍历列表,
* 在迭代过程中修改列表,并获取迭代器在列表中的当前位置。
* ListIterator没有当前元素;
* 其光标位置始终位于调用previous()返回的元素和调用next()返回的元素之间。
* 长度为n的列表的迭代器具有n+1个可能的光标位置,
* 如下面的插入符号(^)所示:
*
* <PRE>
* Element(0) Element(1) Element(2) ... Element(n-1)
* cursor positions: ^ ^ ^ ^ ^
*
* 元素(0) 元素(1) 元素(2) ... 元素(n-1)
* 光标位置: ^ ^ ^ ^ ^
*
* </PRE>
* Note that the {@link #remove} and {@link #set(Object)} methods are
* <i>not</i> defined in terms of the cursor position; they are defined to
* operate on the last element returned by a call to {@link #next} or
* {@link #previous()}.
* 注意,remove和set(Object)方法不是根据光标位置定义的;
* 它们被定义为对调用next或previous()返回的最后一个元素进行操作。
*
*
* <p>This interface is a member of the
* <a href="{@docRoot}/java/util/package-summary.html#CollectionsFramework">
* Java Collections Framework</a>.
*
* @author Josh Bloch
* @see Collection
* @see List
* @see Iterator
* @see Enumeration
* @see List#listIterator()
* @since 1.2
*/
public interface ListIterator<E> extends Iterator<E> {
// Query Operations
// 查询操作
/**
* Returns {@code true} if this list iterator has more elements when
* traversing the list in the forward direction. (In other words,
* returns {@code true} if {@link #next} would return an element rather
* than throwing an exception.)
* 如果此列表迭代器在正向遍历列表时包含更多元素,则返回true。
* (换句话说,如果next将返回元素而不是抛出异常,则返回true。)
*
* @return {@code true} if the list iterator has more elements when
* traversing the list in the forward direction
* 返回 true,如果列表迭代器在正向遍历列表时有更多元素
*/
boolean hasNext();
/**
* Returns the next element in the list and advances the cursor position.
* This method may be called repeatedly to iterate through the list,
* or intermixed with calls to {@link #previous} to go back and forth.
* (Note that alternating calls to {@code next} and {@code previous}
* will return the same element repeatedly.)
* 返回列表中的下一个元素并前进光标位置。可以重复调用此方法来遍历列表,
* 也可以与对previous的调用混合在一起来回调用。
* (请注意,交替调用next和previous将重复返回相同的元素。)
*
*
* @return the next element in the list 列表中的下一个元素
* @throws NoSuchElementException if the iteration has no next element 如果迭代没有下一个元素
*/
E next();
/**
* Returns {@code true} if this list iterator has more elements when
* traversing the list in the reverse direction. (In other words,
* returns {@code true} if {@link #previous} would return an element
* rather than throwing an exception.)
* 如果此列表迭代器在反向遍历列表时有更多元素,则返回true。
* (换句话说,如果previous将返回元素而不是抛出异常,则返回true)
*
*
* @return {@code true} if the list iterator has more elements when
* traversing the list in the reverse direction
* 返回true,如果列表迭代器在反向遍历列表时有更多元素
*/
boolean hasPrevious();
/**
* Returns the previous element in the list and moves the cursor
* position backwards. This method may be called repeatedly to
* iterate through the list backwards, or intermixed with calls to
* {@link #next} to go back and forth. (Note that alternating calls
* to {@code next} and {@code previous} will return the same
* element repeatedly.)
* 返回列表中的上一个元素并向后移动光标位置。可以重复调用此方法以向后遍历列表,
* 也可以与对next的调用混合使用以来回遍历。
* (请注意,交替调用next和previous将重复返回相同的元素。)
*
*
* @return the previous element in the list 列表中的上一个元素
* @throws NoSuchElementException if the iteration has no previous 如果迭代没有以前的元素
* element
*/
E previous();
/**
* Returns the index of the element that would be returned by a
* subsequent call to {@link #next}. (Returns list size if the list
* iterator is at the end of the list.)
* 返回元素的索引,该索引将由对next的后续调用返回。
* (如果列表迭代器位于列表末尾,则返回列表大小。)
*
* @return the index of the element that would be returned by a
* subsequent call to {@code next}, or list size if the list
* iterator is at the end of the list
* 元素的索引,该索引将由对next的后续调用返回,
* 如果列表迭代器位于列表末尾,则返回列表大小
*/
int nextIndex();
/**
* Returns the index of the element that would be returned by a
* subsequent call to {@link #previous}. (Returns -1 if the list
* iterator is at the beginning of the list.)
* 返回元素的索引,该索引将由对previous的后续调用返回。
* (如果列表迭代器位于列表的开头,则返回-1。)
*
* @return the index of the element that would be returned by a
* subsequent call to {@code previous}, or -1 if the list
* iterator is at the beginning of the list
* 元素的索引,该索引将由对previous的后续调用返回,
* 如果列表迭代器位于列表的开头,则返回-1
*/
int previousIndex();
// Modification Operations
// 修改操作
/**
* Removes from the list the last element that was returned by {@link
* #next} or {@link #previous} (optional operation). This call can
* only be made once per call to {@code next} or {@code previous}.
* It can be made only if {@link #add} has not been
* called after the last call to {@code next} or {@code previous}.
* 从列表中删除next或previous返回的最后一个元素(可选操作)。
* 每次对next或previous的调用只能进行一次此调用。
* 只有在上次调用next或previous后未调用add时,才能进行此操作。
*
*
*
* @throws UnsupportedOperationException if the {@code remove}
* operation is not supported by this list iterator
* @throws IllegalStateException if neither {@code next} nor
* {@code previous} have been called, or {@code remove} or
* {@code add} have been called after the last call to
* {@code next} or {@code previous}
* 如果既没有调用next也没有调用previous,
* 或者在上次调用next或previous之后调用了remove或add
*/
void remove();
/**
* Replaces the last element returned by {@link #next} or
* {@link #previous} with the specified element (optional operation).
* This call can be made only if neither {@link #remove} nor {@link
* #add} have been called after the last call to {@code next} or
* {@code previous}.
* 将next或previous返回的最后一个元素替换为指定的元素(可选操作)。
* 只有在上次调用next或previous后remove或add均未被调用时,才能进行此调用。
*
*
* @param e the element with which to replace the last element returned by
* {@code next} or {@code previous} 用来替换next或previous返回的最后一个元素的元素
*
* @throws UnsupportedOperationException if the {@code set} operation
* is not supported by this list iterator 如果此列表迭代器不支持set操作
*
* @throws ClassCastException if the class of the specified element
* prevents it from being added to this list 如果指定元素的类与此列表不兼容
*
* @throws IllegalArgumentException if some aspect of the specified
* element prevents it from being added to this list
* 如果指定元素的某些方面阻止将其添加到此列表中
*
* @throws IllegalStateException if neither {@code next} nor
* {@code previous} have been called, or {@code remove} or
* {@code add} have been called after the last call to
* {@code next} or {@code previous}
* 如果既没有调用next也没有调用previous,
* 或者在上次调用next或previous之后调用了remove或add
*
*/
void set(E e);
/**
* Inserts the specified element into the list (optional operation).
* The element is inserted immediately before the element that
* would be returned by {@link #next}, if any, and after the element
* that would be returned by {@link #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 {@code next} would be unaffected, and a
* subsequent call to {@code previous} would return the new element.
* (This call increases by one the value that would be returned by a
* call to {@code nextIndex} or {@code previousIndex}.)
* 将指定的元素插入列表(可选操作)。该元素直接插入将由next返回的元素之前(如果有),
* 以及将由previous返回的元素之后(如果有)。
* (如果列表不包含任何元素,新元素将成为列表上的唯一元素。)新元素插入隐式游标之前:
* 对next的后续调用将不受影响,对previous的后续调用将返回新元素。
* (此调用将调用nextIndex或previousIndex返回的值增加一个。)
*
* @param e the element to insert
* @throws UnsupportedOperationException if the {@code add} method is
* not supported by this list iterator
* @throws ClassCastException if the class of the specified element
* prevents it from being added to this list
* @throws IllegalArgumentException if some aspect of this element
* prevents it from being added to this list
*/
void add(E e);
}