一.List 接口
List在Collection的基础上增加了有序(插入顺序)的概念,所以在其接口上多了下面带的几个方法:
add(int index, E e), indexOf(Object o),lastindexOf(Object o),ListIterator(int index),remove(int index),set(int index,E e),subList(int fromIndex, int toIndex),get(int index)。在1.8中新加入了replaceAll方法和sort方法。
注意,在collection中没有get方法,因为在coellction中没有序列的概念。还有其中可能改变List中元素或List结构的方法都为可选方法。
API:
二,AbstractList
在这个抽象类中,提供了一些方法的基本实现,方便我们自己实现List(底层为数组存储),要实现不可修改的List,只需提供get和size的实现;要实现可修改的List,提供set方法;要实现可改变结构列表,提供add和remove方法。
AbstractList继承了AbstractCollection且实现了List接口。其中的实现有2个地方比较有意思:1.迭代器;2.subList方法
1.迭代器
在AbstractList中有两类迭代器分别由2个方法iterator()和listIterator()实现。其中listIterator()方法的返回类型ListIterator为iterator()方法返回类型Iterator的子类。在这个类中实现的迭代器方法是给底层为数组存储的List提供的通用迭代器。而在AbstractList的子类AbstractSequentialList中实现的迭代器是为了给底层为链表存储的List提供的通用迭代器。
我们先看下Iterator的实现:
public Iterator<E> iterator() {
return new Itr();
}
private class Itr implements Iterator<E> {
/**
* 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;
public boolean hasNext() {
return cursor != size();
}
public E next() {
checkForComodification();
try {
int i = cursor;
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();
}
}
实现思路:iterator方法返回的迭代器内部维护了两个索引,一个是当前索引cursor,另一个是上一次的索引lastRet。迭代器的索引指向集合中两元素之间,正向迭代的情况下,从第一个集合元素的左端开始,到最后一个集合元素的右端结束。注意一下迭代器remove方法的写法,比较了上一次迭代的索引和当前索引大小,并把lastRet置-1(保证不能连续的remove)。这里加上比较的原因是在它的子类ListIterator的实现类中可以进行两个方向迭代。我们来看下ListIterator的实现类的具体实现:
public ListIterator<E> listIterator() {
return listIterator(0);
}
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();
}
}
}
注意一下,这里的set方法改变的是lastRet(上次访问的元素)。
再看一下subList方法。它返回了一个List的子类。这个子类有两种实现。在这些实现中保存了原List的引用,subList的偏移和subList的大小。利用这些数据在原List上进行操作,所以对subList的修改等同于对原List的修改。这叫做视图。
class SubList<E> extends AbstractList<E> {
private final AbstractList<E> l;
private final int offset;
private int size;