原文:https://blog.youkuaiyun.com/u011240877/article/details/52834074
AbstractList 简介
AbstractList 继承自 AbstractCollection 抽象类,实现了 List 接口 ,是 ArrayList ,Vector和 AbstractSequentiaList 的父类。
在 AbstractCollection 抽象类 中我们知道,AbstractCollection 要求子类必须实现两个方法: iterator () 和 size()。 AbstractList 实现了 iterator()方法:
public Iterator<E> iterator() {
return new Itr();
}
但没有实现 size() 方法
此外还提供了一个抽象方法 get():
abstract public E get(int index);
因此他的子类必须要实现 get(), size() 方法。
实现的方法
1:indexOf(Object) 获取指定对象 首次出现 的索引:
public int indexOf(Object o) {
//获取 ListIterator,此时游标位置为 0
ListIterator<E> it = listIterator();
if (o==null) {
while (it.hasNext())
if (it.next()==null)
//返回游标的前面元素索引
return it.previousIndex();
} else {
while (it.hasNext())
if (o.equals(it.next()))
return it.previousIndex();
}
return -1;
}
当 it.next() == o 时,游标已经在 o 的下一位,所以需要返回 游标的 previousIndex().
因此lastIndexOf(Object) 与之相反,需要从最后一位向前遍历
2:clear(), removeRange(int, int), 全部/范围 删除元素:
public void clear() {
removeRange(0, size());
}
protected void removeRange(int fromIndex, int toIndex) {
ListIterator<E> it = listIterator(fromIndex);
for (int i=0, n=toIndex-fromIndex; i<n; i++) {
it.next();
it.remove();
}
}
两种内部迭代器
Itr 代码分析:
private class Itr implements Iterator<E> {
/**
* 游标
*/
int cursor = 0;
/**
* 上一次迭代到的元素的位置,每次使用完就会置为 -1
*/
int lastRet = -1;
/**
* 用来判断是否发生并发操作的标示,如果这两个值不一致,就会报错
*/
int expectedModCount = modCount;
public boolean hasNext() {
return cursor != size();
}
public E next() {
//检查是否有并发修改操作
checkForComodification();
try {
int i = cursor;
//调用 子类实现的 get() 方法获取元素
E next = get(i);
lastRet = i;
//有迭代操作后就会记录上次迭代的位置
cursor = i + 1;
return next;
} catch (IndexOutOfBoundsException e) {
checkForComodification();
throw new NoSuchElementException();
}
}
public void remove() {
if (lastRet < 0)
throw new IllegalStateException();
checkForComodification();
try {
AbstractList.this.remove(lastRet);
if (lastRet < cursor)
cursor--;
lastRet = -1;
expectedModCount = modCount;
} catch (IndexOutOfBoundsException e) {
throw new ConcurrentModificationException();
}
}
final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
}
ListItr 代码分析:
//ListItr 是 Itr 的增强版
private class ListItr extends Itr implements ListIterator<E> {
//多了个指定游标位置的构造参数
ListItr(int index) {
cursor = index;
}
//除了一开始都有前面元素
public boolean hasPrevious() {
return 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;
}
public void set(E e) {
if (lastRet < 0)
throw new IllegalStateException();
checkForComodification();
try {
AbstractList.this.set(lastRet, e);
expectedModCount = modCount;
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}
public void add(E e) {
checkForComodification();
try {
int i = cursor;
AbstractList.this.add(i, e);
lastRet = -1;
cursor = i + 1;
expectedModCount = modCount;
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}
}
Itr 只是简单实现了 Iterator 的 next, remove 方法。ListItr 在 Itr 基础上多了 向前 和 set 操作。
其他
RandomAccess
public List<E> subList(int fromIndex, int toIndex) {
return (this instanceof RandomAccess ?
new RandomAccessSubList<>(this, fromIndex, toIndex) :
new SubList<>(this, fromIndex, toIndex));
}
RandomAccess 是一个空的接口,它用来标识某个类是否支持 随机访问(随机访问,相对比“按顺序访问”)。一个支持随机访问的类明显可以使用更加高效的算法。
List 中支持随机访问最佳的例子就是 ArrayList, 它的数据结构使得 get(), set(), add()等方法的时间复杂度都是 O(1);
反例就是 LinkedList, 链表结构使得它不支持随机访问,只能按序访问,因此在一些操作上性能略逊一筹。
通常在操作一个 List 对象时,通常会判断是否支持 随机访问,也就是* 是否为 RandomAccess 的实例*,从而使用不同的算法。
比如遍历,实现了 RandomAccess 的集合使用 get():
实现了 RandomAccess 接口的类有:
ArrayList, AttributeList, CopyOnWriteArrayList, Vector, Stack 等。