此类提供 List
接口的骨干实现,以最大限度地减少实现“随机访问”数据存储(如数组)支持的该接口所需的工作。对于连续的访问数据(如链表),应优先使用AbstractSequentialList
,而不是此类。与其他抽象 collection 实现不同,编程人员不必 提供迭代器实现;迭代器和列表迭代器由此类在以下“随机访问”方法上实现:get(int)
、set(int, E)
、add(int, E)
和remove(int)
,下面看看其实现。
//继承于AbstractCollection接口,实现了List接口
public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E> {
protected AbstractList() {//提供一个空的构造函数
}
public boolean add(E e) {//在尾部添加元素e
add(size(), e);//在尾部执行添加操作
return true;
}
//抽象方法
abstract public E get(int index);
//未实现
public E set(int index, E element) {
throw new UnsupportedOperationException();
}
//未实现
public void add(int index, E element) {
throw new UnsupportedOperationException();
}
//未实现
public E remove(int index) {
throw new UnsupportedOperationException();
}
//从头开始,定位值为o的元素
public int indexOf(Object o) {
ListIterator<E> it = listIterator();//获得双向迭代器
if (o==null) {//如果o为空元素
while (it.hasNext())//迭代器遍历
if (it.next()==null)//元素为空
return it.previousIndex();//返回其编号
} else {//如果元素为o
while (it.hasNext())
if (o.equals(it.next()))
return it.previousIndex();
}
return -1;
}
//从尾开始,定位值为o的元素
public int lastIndexOf(Object o) {
ListIterator<E> it = listIterator(size());//获得双向迭代器,迭代器是从最后一个元素开始的
if (o==null) {//如果o为空元素
while (it.hasPrevious())//迭代器遍历
if (it.previous()==null)//元素为空
return it.nextIndex();//返回其编号
} else {
while (it.hasPrevious())
if (o.equals(it.previous()))//比较内容
return it.nextIndex();//返回其编号
}
return -1;
}
public void clear() {//清除所有元素
removeRange(0, size());
}
public boolean addAll(int index, Collection<? extends E> c) {
rangeCheckForAdd(index);//参数有效性检查
boolean modified = false;
for (E e : c) {
add(index++, e);//尾部添加元素
modified = true;
}
return modified;
}
public Iterator<E> iterator() {//返回单向迭代器
return new Itr();
}
public ListIterator<E> listIterator() {//返回双向迭代器
return listIterator(0);
}
public ListIterator<E> listIterator(final int index) {//返回第index位置开始的迭代器
rangeCheckForAdd(index);//参数有效性检查
return new ListItr(index);//返回双向迭代器
}
//单向迭代器的实现,继承了Iterator接口
private class Itr implements Iterator<E> {
int cursor = 0;//迭代器当期标记位
int lastRet = -1;//上次访问的迭代器的标记位
int expectedModCount = modCount;//迭代器迭代期间修改的次数
public boolean hasNext() {//是否还有元素
return cursor != size();//判断标记位是否到达列表末尾
}
//返回下一个元素
public E next() {
checkForComodification();//检查在迭代期间列表大小是否发生变化
try {
int i = cursor;//当前标记位
E next = get(i);//通过列表的get函数获得元素
lastRet = i;//记录老的标记位
cursor = i + 1;//标记位+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--;//当前元素删除之后,当前标记位回退1
lastRet = -1;//老的标记位置位
expectedModCount = modCount;//修正修改次数
} catch (IndexOutOfBoundsException e) {
throw new ConcurrentModificationException();
}
}
//并发修改检查
final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
}
//双向迭代器,继承了单向迭代器,实现了列表迭代器接口
private class ListItr extends Itr implements ListIterator<E> {
ListItr(int index) {//通过索引位来初始化
cursor = index;//当前标记位置为index
}
//是否有前一个元素
public boolean hasPrevious() {
return cursor != 0;
}
//返回前一个元素
public E previous() {
checkForComodification();//并发检查
try {
int i = cursor - 1;
E previous = get(i);
lastRet = cursor = i;//当前标记位,lastRet永远缓存前一个迭代过的元素
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();
}
}
}
//返回列表视图,返回从fromIndex开始到toIndex结束的列表视图,按列表的属性,返回不同的列表
public List<E> subList(int fromIndex, int toIndex) {
return (this instanceof RandomAccess ?new RandomAccessSubList<>(this, fromIndex, toIndex) :
new SubList<>(this, fromIndex, toIndex));
}
//比较List,内部是通过迭代器来比较的
public boolean equals(Object o) {
if (o == this)
return true;
if (!(o instanceof List))
return false;
ListIterator<E> e1 = listIterator();
ListIterator e2 = ((List) o).listIterator();
while (e1.hasNext() && e2.hasNext()) {
E o1 = e1.next();
Object o2 = e2.next();
if (!(o1==null ? o2==null : o1.equals(o2)))
return false;
}
return !(e1.hasNext() || e2.hasNext());
}
//Time31算法
public int hashCode() {
int hashCode = 1;
for (E e : this)
hashCode = 31*hashCode + (e==null ? 0 : e.hashCode());
return hashCode;
}
//删除元素,通过迭代器删除
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();
}
}
protected transient int modCount = 0;
//参数有效性检查
private void rangeCheckForAdd(int index) {
if (index < 0 || index > size())
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
private String outOfBoundsMsg(int index) {
return "Index: "+index+", Size: "+size();
}
}
//子列表,继承了AbstractList
class SubList<E> extends AbstractList<E> {
private final AbstractList<E> l;//内部通过AbstractList来存储元素
private final int offset;//偏移量
private int size;//元素个数
SubList(AbstractList<E> list, int fromIndex, int toIndex) {
if (fromIndex < 0)
throw new IndexOutOfBoundsException("fromIndex = " + fromIndex);
if (toIndex > list.size())
throw new IndexOutOfBoundsException("toIndex = " + toIndex);
if (fromIndex > toIndex)
throw new IllegalArgumentException("fromIndex(" + fromIndex +
") > toIndex(" + toIndex + ")");
l = list;
offset = fromIndex;
size = toIndex - fromIndex;
this.modCount = l.modCount;
}
//第index位添加元素
public E set(int index, E element) {
rangeCheck(index);
checkForComodification();
return l.set(index+offset, element);
}
//获取第index个元素
public E get(int index) {
rangeCheck(index);
checkForComodification();
return l.get(index+offset);
}
//返回元素个数
public int size() {
checkForComodification();
return size;
}
//第index位置添加元素
public void add(int index, E element) {
rangeCheckForAdd(index);//参数有效性检查
checkForComodification();//并发修改检查
l.add(index+offset, element);//按偏移量修正index,之后添加元素
this.modCount = l.modCount;//修改并发修改量
size++;//元素个数+1
}
//删除第index个元素
public E remove(int index) {
rangeCheck(index);
checkForComodification();
E result = l.remove(index+offset);
this.modCount = l.modCount;
size--;
return result;
}
//删除元素
protected void removeRange(int fromIndex, int toIndex) {
checkForComodification();
l.removeRange(fromIndex+offset, toIndex+offset);
this.modCount = l.modCount;
size -= (toIndex-fromIndex);
}
//尾部添加元素
public boolean addAll(Collection<? extends E> c) {
return addAll(size, c);
}
public boolean addAll(int index, Collection<? extends E> c) {
rangeCheckForAdd(index);
int cSize = c.size();
if (cSize==0)
return false;
checkForComodification();
l.addAll(offset+index, c);
this.modCount = l.modCount;
size += cSize;
return true;
}
//返回列表迭代器
public Iterator<E> iterator() {
return listIterator();
}
//返回列表迭代器,这里返回的是经过偏移量修改的列表迭代器,里面的实现和上面描述的一致,这里不详术
public ListIterator<E> listIterator(final int index) {
checkForComodification();
rangeCheckForAdd(index);
return new ListIterator<E>() {
private final ListIterator<E> i = l.listIterator(index+offset);
public boolean hasNext() {
return nextIndex() < size;
}
public E next() {
if (hasNext())
return i.next();
else
throw new NoSuchElementException();
}
public boolean hasPrevious() {
return previousIndex() >= 0;
}
public E previous() {
if (hasPrevious())
return i.previous();
else
throw new NoSuchElementException();
}
public int nextIndex() {
return i.nextIndex() - offset;
}
public int previousIndex() {
return i.previousIndex() - offset;
}
public void remove() {
i.remove();
SubList.this.modCount = l.modCount;
size--;
}
public void set(E e) {
i.set(e);
}
public void add(E e) {
i.add(e);
SubList.this.modCount = l.modCount;
size++;
}
};
}
public List<E> subList(int fromIndex, int toIndex) {
return new SubList<>(this, fromIndex, toIndex);
}
private void rangeCheck(int index) {
if (index < 0 || index >= size)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
private void rangeCheckForAdd(int index) {
if (index < 0 || index > size)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
private String outOfBoundsMsg(int index) {
return "Index: "+index+", Size: "+size;
}
private void checkForComodification() {
if (this.modCount != l.modCount)
throw new ConcurrentModificationException();
}
}
//随机列表,继承了SubList
class RandomAccessSubList<E> extends SubList<E> implements RandomAccess {
RandomAccessSubList(AbstractList<E> list, int fromIndex, int toIndex) {
super(list, fromIndex, toIndex);
}
public List<E> subList(int fromIndex, int toIndex) {
return new RandomAccessSubList<>(this, fromIndex, toIndex);
}
}