上一篇读完了ArrayList的一些基本的方法代码,这一篇接着读一下稍微复杂一点的功能代码。
ArrayList中最常用的功能应该就是遍历了,对于遍历操作,一般使用for循环就可以了,但为了简便操作,从1.8开始多了一个forEach方法。
public void forEach(Consumer<? super E> action) {
Objects.requireNonNull(action);
final int expectedModCount = modCount;
@SuppressWarnings("unchecked")
final E[] elementData = (E[]) this.elementData;
final int size = this.size;
for (int i=0; modCount == expectedModCount && i < size; i++) {
action.accept(elementData[i]);
}
if (modCount != expectedModCount) {
throw new ConcurrentModificationException();
}
}
其中Consumer表示消费一个对象,可以对这个对象做一些操作;forEach方法很简单,就是内部帮你做了一次for循环,让每个对象都被accept方法消费一次,由于可能涉及到对象的修改所以还要判断确保只能是当前线程做的修改,所以需要判断modCount==expectedModCount;
我们都知道,除了for循环外,还可以使用迭代器来进行遍历。ArrayList有三个迭代方法分别是从最顶层Iterable接口,和List接口继承来的。Iterator迭代器是通用的迭代器,为了更友好,也更方便的操作List,所以有了针对于List的迭代器ListIterator。
// 省略其他方法
public interface Iterable<T> {
Iterator<T> iterator();
}
public interface List<E> extends Collection<E> {
ListIterator<E> listIterator();
ListIterator<E> listIterator(int index);
}
其中Iterator和ListIterator的继承关系如下
那么先来看看,ArrayList中是如何实现 iterator方法的。
public Iterator<E> iterator() {
return new Itr();
}
private class Itr implements Iterator<E> {
int cursor; // index of next element to return
int lastRet = -1; // index of last element returned; -1 if no such
int expectedModCount = modCount;
Itr() {}
public boolean hasNext() {
return cursor != size;
}
@SuppressWarnings("unchecked")
public E next() {
checkForComodification();
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];
}
public void remove() {
if (lastRet < 0)
throw new IllegalStateException();
checkForComodification();
try {
ArrayList.this.remove(lastRet);
cursor = lastRet;
lastRet = -1;
expectedModCount = modCount;
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}
@Override
@SuppressWarnings("unchecked")
public void forEachRemaining(Consumer<? super E> consumer) {
Objects.requireNonNull(consumer);
final int size = ArrayList.this.size;
int i = cursor;
if (i >= size) {
return;
}
final Object[] elementData = ArrayList.this.elementData;
if (i >= elementData.length) {
throw new ConcurrentModificationException();
}
while (i != size && modCount == expectedModCount) {
consumer.accept((E) elementData[i++]);
}
// update once at end of iteration to reduce heap write traffic
cursor = i;
lastRet = i - 1;
checkForComodification();
}
final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
}
很显然直接new了一个私有内部类Itr,Itr实现了Iterator接口,所以他是一个ArrayList私有的迭代器。再来看看他是如何具体实现Iterator接口的。首先,Itr有三个成员变量
cursor:游标,用于存储下一个需要返回元素的下标;
lastRet:存储上一次返回的元素的下标;
expectedModCount:期望的操作数,和ArrayList的modCount一致;
再来看具体方法
checkForComodification:校验是否有其他线程在做操作;如果modCount != expectedModCount就报异常
hasNext:是否还有下一个元素,很容易理解
next:返回下一个元素;先校验线程操作再校验cursor是否越界,有则抛异常;否则维护lastRet = cursro,cursor+=1,将处于lastRet位置的值返回出去;
remove:删除上一个返回的元素;先校验,再调用ArrayList的remove(index)方法,删除lastRet位置的元素,然后再维护变量的值。
forEachRemaining:对剩下未迭代的元素进行一次遍历,并对每一个元素进行一次消费,然后维护cursor和lastRet变量就好了。
接着来看listIterator 和 listIterator(index)方法,也是直接返回的私有内部类ListItr
public ListIterator<E> listIterator(int index) {
if (index < 0 || index > size)
throw new IndexOutOfBoundsException("Index: "+index);
return new ListItr(index);
}
public ListIterator<E> listIterator() {
return new ListItr(0);
}
private class ListItr extends Itr implements ListIterator<E> {
ListItr(int index) {
super();
cursor = index;
}
public boolean hasPrevious() {
return cursor != 0;
}
public int nextIndex() {
return cursor;
}
public int previousIndex() {
return cursor - 1;
}
@SuppressWarnings("unchecked")
public E previous() {
checkForComodification();
int i = cursor - 1;
if (i < 0)
throw new NoSuchElementException();
Object[] elementData = ArrayList.this.elementData;
if (i >= elementData.length)
throw new ConcurrentModificationException();
cursor = i;
return (E) elementData[lastRet = i];
}
public void set(E e) {
if (lastRet < 0)
throw new IllegalStateException();
checkForComodification();
try {
ArrayList.this.set(lastRet, e);
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}
public void add(E e) {
checkForComodification();
try {
int i = cursor;
ArrayList.this.add(i, e);
cursor = i + 1;
lastRet = -1;
expectedModCount = modCount;
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}
}
可以看到ListItr继承了Itr所以是功能更加丰富的Itr。其构造函数必须传入一个index,表示必须告诉迭代器从哪个下标开始迭代。
hasPrevious:判断前面是否有元素;
nextIndex:返回下一个要迭代的下标,即cursor;
previousIndex:上一个元素的下标,即cursor-1;
previous:返回前一个元素;即返回cursor-1位置的元素,把cursor往前移了一下,这说明ListItr可以从后往前进行迭代。
set:在lastRet位置设置指定元素;先校验,再调用的ArrayList的set方法。
add:在cursor位置添加指定元素;先校验,再调用ArrayList的add方法,在cursor位置添加元素,然后cursor+1,维护lastRet和expectedModCount。
除了迭代操作之外,有很多时候也需要把集合进行截取,所以subList方法也是相对用的较多的方法。
public List<E> subList(int fromIndex, int toIndex) {
subListRangeCheck(fromIndex, toIndex, size);
return new SubList(this, 0, fromIndex, toIndex);
}
static void subListRangeCheck(int fromIndex, int toIndex, int size) {
if (fromIndex < 0)
throw new IndexOutOfBoundsException("fromIndex = " + fromIndex);
if (toIndex > size)
throw new IndexOutOfBoundsException("toIndex = " + toIndex);
if (fromIndex > toIndex)
throw new IllegalArgumentException("fromIndex(" + fromIndex +
") > toIndex(" + toIndex + ")");
}
private class SubList extends AbstractList<E> implements RandomAccess {
private final AbstractList<E> parent;
private final int parentOffset;
private final int offset;
int size;
SubList(AbstractList<E> parent,
int offset, int fromIndex, int toIndex) {
this.parent = parent;
this.parentOffset = fromIndex;
this.offset = offset + fromIndex;
this.size = toIndex - fromIndex;
this.modCount = ArrayList.this.modCount;
}
public E set(int index, E e) {
rangeCheck(index);
checkForComodification();
E oldValue = ArrayList.this.elementData(offset + index);
ArrayList.this.elementData[offset + index] = e;
return oldValue;
}
public E get(int index) {
rangeCheck(index);
checkForComodification();
return ArrayList.this.elementData(offset + index);
}
public int size() {
checkForComodification();
return this.size;
}
public void add(int index, E e) {
rangeCheckForAdd(index);
checkForComodification();
parent.add(parentOffset + index, e);
this.modCount = parent.modCount;
this.size++;
}
public E remove(int index) {
rangeCheck(index);
checkForComodification();
E result = parent.remove(parentOffset + index);
this.modCount = parent.modCount;
this.size--;
return result;
}
protected void removeRange(int fromIndex, int toIndex) {
checkForComodification();
parent.removeRange(parentOffset + fromIndex,
parentOffset + toIndex);
this.modCount = parent.modCount;
this.size -= toIndex - fromIndex;
}
public boolean addAll(Collection<? extends E> c) {
return addAll(this.size, c);
}
public boolean addAll(int index, Collection<? extends E> c) {
rangeCheckForAdd(index);
int cSize = c.size();
if (cSize==0)
return false;
checkForComodification();
parent.addAll(parentOffset + index, c);
this.modCount = parent.modCount;
this.size += cSize;
return true;
}
// AbstractList.listIterator() -> this.listIterator(0)
public Iterator<E> iterator() {
return listIterator();
}
public ListIterator<E> listIterator(final int index) {
checkForComodification();
rangeCheckForAdd(index);
final int offset = this.offset;
return new ListIterator<E>() {
int cursor = index;
int lastRet = -1;
int expectedModCount = ArrayList.this.modCount;
public boolean hasNext() {
return cursor != SubList.this.size;
}
@SuppressWarnings("unchecked")
public E next() {
checkForComodification();
int i = cursor;
if (i >= SubList.this.size)
throw new NoSuchElementException();
Object[] elementData = ArrayList.this.elementData;
if (offset + i >= elementData.length)
throw new ConcurrentModificationException();
cursor = i + 1;
return (E) elementData[offset + (lastRet = i)];
}
public boolean hasPrevious() {
return cursor != 0;
}
@SuppressWarnings("unchecked")
public E previous() {
checkForComodification();
int i = cursor - 1;
if (i < 0)
throw new NoSuchElementException();
Object[] elementData = ArrayList.this.elementData;
if (offset + i >= elementData.length)
throw new ConcurrentModificationException();
cursor = i;
return (E) elementData[offset + (lastRet = i)];
}
@SuppressWarnings("unchecked")
public void forEachRemaining(Consumer<? super E> consumer) {
Objects.requireNonNull(consumer);
final int size = SubList.this.size;
int i = cursor;
if (i >= size) {
return;
}
final Object[] elementData = ArrayList.this.elementData;
if (offset + i >= elementData.length) {
throw new ConcurrentModificationException();
}
while (i != size && modCount == expectedModCount) {
consumer.accept((E) elementData[offset + (i++)]);
}
// update once at end of iteration to reduce heap write traffic
lastRet = cursor = i;
checkForComodification();
}
public int nextIndex() {
return cursor;
}
public int previousIndex() {
return cursor - 1;
}
public void remove() {
if (lastRet < 0)
throw new IllegalStateException();
checkForComodification();
try {
SubList.this.remove(lastRet);
cursor = lastRet;
lastRet = -1;
expectedModCount = ArrayList.this.modCount;
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}
public void set(E e) {
if (lastRet < 0)
throw new IllegalStateException();
checkForComodification();
try {
ArrayList.this.set(offset + lastRet, e);
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}
public void add(E e) {
checkForComodification();
try {
int i = cursor;
SubList.this.add(i, e);
cursor = i + 1;
lastRet = -1;
expectedModCount = ArrayList.this.modCount;
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}
final void checkForComodification() {
if (expectedModCount != ArrayList.this.modCount)
throw new ConcurrentModificationException();
}
};
}
public List<E> subList(int fromIndex, int toIndex) {
subListRangeCheck(fromIndex, toIndex, size);
return new SubList(this, offset, fromIndex, toIndex);
}
private void rangeCheck(int index) {
if (index < 0 || index >= this.size)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
private void rangeCheckForAdd(int index) {
if (index < 0 || index > this.size)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
private String outOfBoundsMsg(int index) {
return "Index: "+index+", Size: "+this.size;
}
private void checkForComodification() {
if (ArrayList.this.modCount != this.modCount)
throw new ConcurrentModificationException();
}
public Spliterator<E> spliterator() {
checkForComodification();
return new ArrayListSpliterator<E>(ArrayList.this, offset,
offset + this.size, this.modCount);
}
}
可以看到subList方法其实使用的ArrayList的内部类SubList。SubList的构造函数需要四个参数
parent:父List,即要从哪个List来截取。
fromIndex:也叫parentOffset,即从父List的哪个下标开始截取。
offset:偏移量,从fromIndex开始再偏移offset个位置开始截取。
toIndex:到哪个下标为止。
这四个参数都转化成了SubList内部的变量,这些变量都是相对于ArrayList来的。而且可以看到SubList内部的这些方法实现其实调用的都是ArrayList的方法,只是做了一些简单的校验,和ArrayList具体实现做的校验差不多。为了在使用上看起来像是在操作一个新的数组,所以增删改查的操作都要在增加一个offset的偏移量,而不能直接使用index。
虽然大部分都是调用的ArrayList方法,但也有一些方法是自己实现的,比如iterator方法,这个方法调用的父类 AbstractList 的 listIterator ,再调用 listIterator(0)方法,这个方法就是SubList自己实现的,最终返回的是一个匿名内部类,可以说SubList只有一个迭代器那就是ListIterator。迭代器的具体实现和ArrayList的实现原理大致是一样的,只是在对于下标的处理上总是要注意一下偏移量。
可以看到SubList内部一直使用的是ArrayList的elementData,所以他们对数组的修改会影响彼此。
到这里基本上ArrayList的源码都已经读的差不多了,还有一些像ArrayListSpliterator这样的类,等以后有时间再去好好研究研究吧。