ListIterator是List迭代器,该接口继承Iterator,有关Iterator接口请看:
http://blog.csdn.net/treeroot/archive/2004/09/11/101589.aspx
这里列出所有的方法(包括在Iterator中的方法,Java源码中也常常这么做,虽然不是
必须呢,但是看起来确实比较方便).
public interface ListIterator extends Iterator
1.boolean hasNext()
从父接口继承
2. boolean next()
从父接口继承
3.boolean hasPrevious()
很容易理解,前面是否还有元素
4.Object previous()
返回前一个元素(next()返回的前一个)
5.int nextIndex()
下一个元素的索引号
6.int previousIndex()
上一个元素的索引号
7.void remove()
从父接口继承,和Iterator有点区别,因为可以是next()或者
previous()返回的元素,其他都一样
8.void set(Object o)
替换最后一次调用next()或者previous()的元素.
9.void add(Object o)
添加一个元素(在next()返回的元素之前,previous()返回的元素之后)
为了说明问题,这里把JDK中的部分注释摘抄如下
/**
*
* 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 {@link #remove} and {@link #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 {@link #next} or {@link
* #previous()}.
*/
大致意思是:ListIterator是一个双向迭代器。ListIterator没有当前元素,它的当前游标是位于
调用next()和previsous()返回的元素之间。不过下面举的例子有点问题:下面的例子是n+1个元素。
如果有n个元素,那么游标索引就是0...n共n+1个。
注意:romove和set方法不是针对当前游标的操作,而是针对最后一次的next()或者previous()调用。
因为只是一个接口就不举例说明了。