最近在看《on java 8》,到下面迭代器的方法时很困惑,遂上网查阅相关资料,有所收获和自己的理解这里分享一下
具体代码如下:
public class ListIteration {
public static void main(String[] args) {
List<Pet> pets = Pets.list(8);
ListIterator<Pet> it = pets.listIterator();
while(it.hasNext())
System.out.print(it.next() +
", " + it.nextIndex() +
", " + it.previousIndex() + "; ");
System.out.println();
// Backwards:
while(it.hasPrevious())
System.out.print(it.previous().id() + " ");
System.out.println();
System.out.println(pets);
it = pets.listIterator(3);
while(it.hasNext()) {
it.next();
it.set(Pets.get());
}
System.out.println(pets);
}
}
/* Output:
Rat, 1, 0; Manx, 2, 1; Cymric, 3, 2; Mutt, 4, 3; Pug,
5, 4; Cymric, 6, 5; Pug, 7, 6; Manx, 8, 7;
7 6 5 4 3 2 1 0
[Rat, Manx, Cymric, Mutt, Pug, Cymric, Pug, Manx]
[Rat, Manx, Cymric, Cymric, Rat, EgyptianMau, Hamster,
EgyptianMau]
*/
我们可以看到第一行输出Rat,1,0 ;Manx,2,1
这与我想的Rat,1,-1 ; Manx,2,0
有所不同,所以找了下原因,结果发现是next()方法和nextIndex()的问题
源码如下:
public E next() {
checkForComodification();
//lastRet初始值为-1
int i = cursor;
if (i >= size)
throw new NoSuchElementException();
Object[] elementData = ArrayList.this.elementData;
if (i >= elementData.length)
throw new ConcurrentModificationException();
cursor = i + 1;//将游标后移一位
return (E) elementData[lastRet = i];//i 存储的是游标cursor改变之前的值
}
public int nextIndex() {
return cursor;
}
public int previousIndex() {
return cursor - 1;
}
这里我们需要知道 lastRet 变量初始值为-1,在next()方法里,程序先保留了初始下标,再使下标加一,然后返回初始下标对应的对象。
再来看一个例子:
1 while(it.hasNext()){
2 it.remove();
3 it.next();
4 }
这个例子会输出数组越界的错误,为什么呢?
因为这里是直接remove()了lastRet=-1下标所对应的元素,所以出现错误
正确使用应该这样
1 while(it.hasNext()){
2 it.next();
3 it.remove();
4 }
这里next()方法做了两件事:
1.改变了lastRet的值,使之变为当前第一个元素所对应的下标,并返回当前对象
2.使下标+1
所以我们在这里就可以解释清我在《on java 8》里遇到的问题:
在程序中:
- 先调用next(),使下标指向0,并输出其对应的元素,下标+1
(此时下标为0+1=1) - 再nextIndex(),返回当前指针对应的下标大小,即为1
- 最后previousIndex(),返回下标-1的大小,即为0
完事!