您的位置:首页 > 编程语言 > Java开发

解析java.util集合类源码(AbstractList内部类ListIterator和Itr)

2014-01-20 11:33 323 查看
在AbstractList中用主要用Iterator和ListIterator来对列表进行遍历,但在AbstractList也没有声明一个Iterator对象,而是用一个内部类来实现这个功能。

首先在AbstractList中,有两个返回迭代器的方法

public ListIterator<E> listIterator()

public Iterator<E> iterator()

在iterator方法中

public Iterator<E> iterator() {
return new Itr();
}


返回了一个Itr类的对象,这是AbstractList中的内部类,实现了Iterator接口

AbstractList内部类Itr

/**
* Index of element to be returned by subsequent call to next.
*/
int cursor = 0;

/**
* Index of element returned by most recent call to next or
* previous.  Reset to -1 if this element is deleted by a call
* to remove.
*/
int lastRet = -1;

/**
* The modCount value that the iterator believes that the backing
* List should have.  If this expectation is violated, the iterator
* has detected concurrent modification.
*/
int expectedModCount = modCount;
在Itr中有三个属性域

cursor 迭代时候标识当前将要遍历元素的下标

lastRet是列表中由next或者previous方法最后访问指向的下标

expectedModCount 这个属性最重要,在AbstractList中有modCount属性,这两个属性主要用来辨别在程序中,返回了Iterator迭代器之后,该列表是否又进行了修改(如添加、删除),当列表进行了这些操作之后,modCount会改变,但是expectedModCount没有变,当modCout!=expectedModCount报异常,像下面的代码

@Test
public void test(){

ArrayList list = new ArrayList();
list.add("ee");
list.add("ee");
list.add("eee");
Iterator it = list.listIterator();
list.add("在这里会修改modCount");
it.next(); //这会报错,原因modCount与expectedModCount值不同

}
也就是说当列表在进行迭代时候不允许列表添加(删除)等操作

public boolean hasNext() {
return cursor != size();
}
调用子类实现的size()方法,判断是否还有下一个元素

final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}


checkForComodification方法中判断modCount和expectedmodCount是否相等,不相等报异常,因为此类中的每个方法基本都要用这个方法,所以先写出来

public E next() {
checkForComodification();
try {
E next = get(cursor);
lastRet = cursor++;
return next;
} catch (IndexOutOfBoundsException e) {
checkForComodification();
throw new NoSuchElementException();
}
}
先调用checkForComodification检查,列表是否被修改了,调用子类实现get方法,给lstRet和cursor重新赋值

public void remove() {
if (lastRet == -1)
throw new IllegalStateException();
checkForComodification();

try {
AbstractList.this.remove(lastRet);
if (lastRet < cursor)
cursor--;
lastRet = -1;
expectedModCount = modCount;
} catch (IndexOutOfBoundsException e) {
throw new ConcurrentModificationException();
}
}
使用remove方法,必须先使用next方法,remove删除的是当前指向的那个对象,若还没有用next方法,使lastRet初始化,lastRet的值为-1抛出异常,然后检查列表修改

调用子类实现的remove方法移除对象,对lastRest和cursor重新赋值

AbstractList内部类ListItr

ListItr实现了ListIterator接口同时继承了Itr,此接口是Iterator接口的扩展,使迭代器不但可以向后遍历,也可以向前遍历,还可以获取将要遍历元素的下标,

和当前元素的下标,同时可以通过迭代器对元素进行添加、删除、修改的操作



public boolean hasPrevious() {
return cursor != 0;
}


通过判断cursor是否为0 ,来辨别在当前元素前是否还有元素可以遍历,当cursor为0 ,表明已经到了列表开始的位置

public E previous() {
checkForComodification();
try {
int i = cursor - 1;
E previous = get(i);
lastRet = cursor = i;
return previous;
} catch (IndexOutOfBoundsException e) {
checkForComodification();
throw new NoSuchElementException();
}
}
返回当前元素的前一元素

public int nextIndex() {
return cursor;
}

public int previousIndex() {
return cursor-1;
}
返回当前将要遍历元素的下标  和   返回对 previous 的后续遍历所返回元素的下标

public void set(E e) {
if (lastRet == -1)
throw new IllegalStateException();
checkForComodification();

try {
AbstractList.this.set(lastRet, e);
expectedModCount = modCount;
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}
对next或previous方法返回的最后一个元素进行替换

public void add(E e) {
checkForComodification();

try {
AbstractList.this.add(cursor++, e);
lastRet = -1;
expectedModCount = modCount;
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}
将指定的元素插入列表到列表将要遍历的位置,同时将lastRet置-1,表明刚才的操作可能会使lastRet指向新插入的元素而不是原来指向的元素
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息