1.底层实现
private static final long serialVersionUID = 8683452581122892189L;
private static final int DEFAULT_CAPACITY = 10;//默认初始容量
private static final Object[] EMPTY_ELEMENTDATA = {};//空数组
private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};//默认空数组1
transient Object[] elementData; // 保存数据的数组,transient表示表示不会被序列化
private int size;//ArrayList实际元素数量,,需要注意的是elementData.length!=size(即数组容量不等于集合实际元素数量)
public ArrayList(int initialCapacity) {//带容量大小的构造函数
if (initialCapacity > 0) {
this.elementData = new Object[initialCapacity];
} else if (initialCapacity == 0) {
this.elementData = EMPTY_ELEMENTDATA;
} else {
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
}
}
public ArrayList() {//无参构造函数
this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
}
public ArrayList(Collection<? extends E> c) {//构造参数为Collection对象实例,生成的
elementData = c.toArray();
if ((size = elementData.length) != 0) {
if (elementData.getClass() != Object[].class)
elementData = Arrays.copyOf(elementData, size, Object[].class);
} else {
this.elementData = EMPTY_ELEMENTDATA;
}
}
ArrayList是基于数组实现,本质上是对象引用的一个变长数组,能够动态的增加或减小其大小。为什么这么说,,来看下面ArrayList是如何扩容就知道了。
2.扩容
public boolean add(E e) {//添加元素
ensureCapacityInternal(size + 1);
elementData[size++] = e;
return true;
}
public void add(int index, E element) {//添加元素到指定位置
rangeCheckForAdd(index);
ensureCapacityInternal(size + 1);
System.arraycopy(elementData, index, elementData, index + 1,
size - index);
elementData[index] = element;
size++;
}
可以看到两个添加元素的方法中都用到了ensureCapacityInternal()方法;
private void ensureCapacityInternal(int minCapacity) {//这里的minCapacity是上面的size+1,即添加元素后的实际元素数量
if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);//取默认容量与实际元素数量的较大值
}
ensureExplicitCapacity(minCapacity);
}
private void ensureExplicitCapacity(int minCapacity) {
modCount++;
if (minCapacity - elementData.length > 0)//如果实际元素数量大于数组容量,则对数组进行扩容
grow(minCapacity);
}
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;//数组最大容量2^31-9
private void grow(int minCapacity) {
int oldCapacity = elementData.length;//原先的数组容量
int newCapacity = oldCapacity + (oldCapacity >> 1);//数组扩容为原先容量的1.5倍
if (newCapacity - minCapacity < 0)//取扩容后数组容量与实际元素数量的较大值
newCapacity = minCapacity;
if (newCapacity - MAX_ARRAY_SIZE > 0)
newCapacity = hugeCapacity(minCapacity);//通过hugeCapacity()方法得知,数组最大容量为Integer.MAX_VALUE,即2^31-1;
elementData = Arrays.copyOf(elementData, newCapacity);//Arrays.copyOf()这个方法经常会用到,,这里的作用就是将原先的数组元素放到扩容后的数组当中
}
private static int hugeCapacity(int minCapacity) {
if (minCapacity < 0)
throw new OutOfMemoryError();
return (minCapacity > MAX_ARRAY_SIZE) ?
Integer.MAX_VALUE :
MAX_ARRAY_SIZE;
}
简单来看看Arrays.copyOf()方法;
public static <T> T[] copyOf(T[] original, int newLength) {
return (T[]) copyOf(original, newLength, original.getClass());
}
public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
@SuppressWarnings("unchecked")
T[] copy = ((Object)newType == (Object)Object[].class)
? (T[]) new Object[newLength]
: (T[]) Array.newInstance(newType.getComponentType(), newLength);
System.arraycopy(original, 0, copy, 0,
Math.min(original.length, newLength));
return copy;
}
public static native void arraycopy(Object src, int srcPos,
Object dest, int destPos,
int length);
可以看到Arrays.copyOf()的最终实现是通过JNI方法System.arraycopy()实现,,目的就是将原数组的指定元素复制到目标数组的指定位置;
3.遍历实现
在说集合遍历之前先撸击几串代码;
public static void removeListElement(){
List<String> list=new ArrayList<String>();
list.add("a");
list.add("b");
list.add("b");
list.add("c");
for(int i=0;i<list.size();i++){
if(list.get(i).equals("b")){
list.remove(i);
}
}
for(int i=0;i<list.size();i++){
System.out.println(list.get(i));
}
System.out.println(list);
}
打印结果:a
b
c
[a, b, c]
public static void removeListElement1(){
List<String> list=new ArrayList<String>();
list.add("a");
list.add("b");
list.add("b");
list.add("c");
for(String str: list){
if(str.equals("b")){
list.remove(str);
}
}
for(int i=0;i<list.size();i++){
System.out.println(list.get(i));
}
System.out.println(list);
}
打印结果:Exception in thread "main" java.util.ConcurrentModificationException
public static void removeListElement2(){
List<String> list=new ArrayList<String>();
list.add("a");
list.add("b");
list.add("b");
list.add("c");
Iterator it=list.iterator();
while(it.hasNext()){
if(it.next().equals("b")){
it.remove();
}
}
for(int i=0;i<list.size();i++){
System.out.println(list.get(i));
}
System.out.println(list);
}
打印结果:a
c
[a, c]
上面列举了三种遍历集合,并在遍历过程中删除集合元素打印结果;
有几点需要说明:
1.forEach()的本质是迭代器,foreach语法最终被编译器转为了对Iterator.hasNext()和对Iterator.next()的调用;
具体可以参见这篇博客:https://yq.aliyun.com/articles/40357
2.集合的toString()方法也是通过迭代器实现;
贴上AbstractCollection的toString方法:
public String toString() {
Iterator<E> it = iterator();
if (! it.hasNext())
return "[]";
StringBuilder sb = new StringBuilder();
sb.append('[');
for (;;) {
E e = it.next();
sb.append(e == this ? "(this Collection)" : e);
if (! it.hasNext())
return sb.append(']').toString();
sb.append(',').append(' ');
}
}
3.modCount用于记录ArrayList集合的修改次数,迭代器通过modCount变量来保证每次迭代的时候集合没有发生改变;
public E remove(int index) {
rangeCheck(index);
modCount++;
E oldValue = elementData(index);
int numMoved = size - index - 1;
if (numMoved > 0)
System.arraycopy(elementData, index+1, elementData, index,
numMoved);
elementData[--size] = null; // clear to let GC do its work
return oldValue;
}
public boolean remove(Object o) {
if (o == null) {
for (int index = 0; index < size; index++)
if (elementData[index] == null) {
fastRemove(index);
return true;
}
} else {
for (int index = 0; index < size; index++)
if (o.equals(elementData[index])) {
fastRemove(index);
return true;
}
}
return false;
}
private void fastRemove(int index) {
modCount++;
int numMoved = size - index - 1;
if (numMoved > 0)
System.arraycopy(elementData, index+1, elementData, index,
numMoved);
elementData[--size] = null; // clear to let GC do its work
}
removeListElement()方法:
每次删除元素modCount++,,但是用get()方法取元素的时候,并没有检查modCount变量是否与上次一致,所以没有像removeListElement1()方法一样抛异常;
因为每一次删除元素的时候,会调用System.arraycopy(elementData, index+1, elementData, index,numMoved)方法,来对被删除元素的所有后续元素做整体向前移动一位的偏移操作,所以当i=1,删除第一个"b",,并且后续的"b"、"c"元素整体向前位移一个单位,,等到i=2时,list[2]=c,,因此只删除掉了一个"b",第二个"b"逃过一劫;
要解释removeListElement()1中为什么抛java.util.ConcurrentModificationException异常,就需要看看ArrayList中迭代器的实现了:
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);
}
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;
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();
}
}
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();
}
}
}
从源码中可以看到,ArrayList中通过内部类实现了两个迭代器,暂且称为:传统迭代器Itr和个性化迭代器ListItr,ListItr继承了Itr,并添加了一些新功能,如:添加元素,取上一个元素,获取cursor游标的位置等等;可以看到在生成迭代器的时候,会用exceptedModCount来存储当前集合中的modCount变量;
int expectedModCount = modCount;
然后每次遍历操作的时候,都会调用checkForComodification()方法,来检查当前集合是否被改变;
final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
一旦集合做了add或者remove等操作,改变了集合原有结构时modCount++,modCount会发生改变;而此时迭代器进行下一次迭代时,会导致expectedModCount与modCount不一致,,从而抛出ConcurrentModificationException异常;
但是通过迭代器来做删除元素操作时,每次在删除元素后,都会将改变后的modCount重新赋给expectedModCount,从而使得迭代器下一次迭代时,再次调用checkForComodification()检查便不会抛出异常;
4.常用方法详解
subList()方法:用来返回一个list的一部分的视图(暂且这么称呼吧),依赖于内部类SubList实现,是因为实际上返回的list是靠原来的list支持的;
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;
}
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也引用了与迭代器相似的机制;this.modCount = ArrayList.this.modCount;
SubList构造方法中的这行代码,将外部类的modCount变量付给了内部类SubList对象实例的modCount,即将原集合的modCount赋给视图的modCount;
然后每次对视图进行操作的时候,,都会调用checkForComodification()方法检查,通过比较视图的modCount与原集合的modCount是否一致,来判断原集合是否有被改变,如有,则抛ConcurrentModificationException异常;
final void checkForComodification() {
if (expectedModCount != ArrayList.this.modCount)
throw new ConcurrentModificationException();
}
public void trimToSize() {
modCount++;
if (size < elementData.length) {
elementData = (size == 0)
? EMPTY_ELEMENTDATA
: Arrays.copyOf(elementData, size);
}
}
removeAll()、retainAll()与batchRemove()方法:取交集与取余集
public boolean removeAll(Collection<?> c) {
Objects.requireNonNull(c);
return batchRemove(c, false);
}
public boolean retainAll(Collection<?> c) {
Objects.requireNonNull(c);
return batchRemove(c, true);
}
private boolean batchRemove(Collection<?> c, boolean complement) {
final Object[] elementData = this.elementData;
int r = 0, w = 0;
boolean modified = false;
try {
for (; r < size; r++)
if (c.contains(elementData[r]) == complement)
elementData[w++] = elementData[r];
} finally {
// Preserve behavioral compatibility with AbstractCollection,
// even if c.contains() throws.
if (r != size) {
System.arraycopy(elementData, r,
elementData, w,
size - r);
w += size - r;
}
if (w != size) {
// clear to let GC do its work
for (int i = w; i < size; i++)
elementData[i] = null;
modCount += size - w;
size = w;
modified = true;
}
}
return modified;
}
removeAll()与retainAll()都是通过batchRemove()方法实现,removeAll()可以用来取余集,retainAll()可以用来取交集;