概述
实现了List接口,基于数组实现,元素存储有序并可随机访问,线程不安全。每个ArrayList都有一个容量(capacity),表示底层数组的实际大小。当ArrayList元素增加到容量时,创建一个新数组,并将原有数组中的元素通过Arrays.copyOf()克隆到新数组中。
实现
数据结构
transient Object[] elementData; // non-private to simplify nested class access
构造函数
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);
}
}
/**
* Constructs an empty list with an initial capacity of ten.
*/
public ArrayList() {
this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
}
/**
* Constructs a list containing the elements of the specified
* collection, in the order they are returned by the collection's
* iterator.
*
* @param c the collection whose elements are to be placed into this list
* @throws NullPointerException if the specified collection is null
*/
public ArrayList(Collection<? extends E> c) {
elementData = c.toArray();
if ((size = elementData.length) != 0) {
// c.toArray might (incorrectly) not return Object[] (see 6260652)
if (elementData.getClass() != Object[].class)
elementData = Arrays.copyOf(elementData, size, Object[].class);
} else {
// replace with empty array.
this.elementData = EMPTY_ELEMENTDATA;
}
}
增
add(E e)
在数组列表最后面添加元素。首先判断添加该元素后集合长度是否超过数组长度,如果超过则对数组进行扩容,扩容后再次进行添加。
private void add(E e, Object[] elementData, int s) {
if (s == elementData.length)
elementData = grow();
elementData[s] = e;
size = s + 1;
}
add(int index, E element)
在数组指定位置添加元素。首先判断添加该元素后集合长度是否超过数组长度,如果超过则对数组进行扩容,扩容后将指定位置后的元素统一向后移动(System.arraycopy),最后在指定位置添加元素。
public void add(int index, E element) {
rangeCheckForAdd(index);
modCount++;
final int s;
Object[] elementData;
if ((s = size) == (elementData = this.elementData).length)
elementData = grow();
System.arraycopy(elementData, index,
elementData, index + 1,
s - index);
elementData[index] = element;
size = s + 1;
}
addAll(Collection<? extends E> c)
在数组列表最后面添加集合。首先判断添加该集合后总长度是否超过数组长度,如果超过则对数组进行扩容,扩容后使用System.arraycopy将元素复制过来。
public boolean addAll(Collection<? extends E> c) {
Object[] a = c.toArray();
modCount++;
int numNew = a.length;
if (numNew == 0)
return false;
Object[] elementData;
final int s;
if (numNew > (elementData = this.elementData).length - (s = size))
elementData = grow(s + numNew);
System.arraycopy(a, 0, elementData, s, numNew);
size = s + numNew;
return true;
}
addAll(int index, Collection<? extends E> c)
在数组指定位置添加集合。首先判断添加该集合后总长度是否超过数组长度,如果超过则对数组进行扩容,扩容后将指定位置后的元素统一向后移动(System.arraycopy),最后在指定位置使用使用System.arraycopy将集合中元素复制过来。
public boolean addAll(int index, Collection<? extends E> c) {
rangeCheckForAdd(index);
Object[] a = c.toArray();
modCount++;
int numNew = a.length;
if (numNew == 0)
return false;
Object[] elementData;
final int s;
if (numNew > (elementData = this.elementData).length - (s = size))
elementData = grow(s + numNew);
int numMoved = s - index;
if (numMoved > 0)
System.arraycopy(elementData, index,
elementData, index + numNew,
numMoved);
System.arraycopy(a, 0, elementData, index, numNew);
size = s + numNew;
return true;
}
删
remove(Object o)
删除第一个满足equals方法的对象
public boolean remove(Object o) {
final Object[] es = elementData;
final int size = this.size;
int i = 0;
found: {
if (o == null) {
for (; i < size; i++)
if (es[i] == null)
break found;
} else {
for (; i < size; i++)
if (o.equals(es[i]))
break found;
}
return false;
}
fastRemove(es, i);
return true;
}
remove(int index)
删除指定位置的对象
public E remove(int index) {
Objects.checkIndex(index, size);
final Object[] es = elementData;
@SuppressWarnings("unchecked") E oldValue = (E) es[index];
fastRemove(es, index);
return oldValue;
}
以上两种方法最终调用的是fastRemove方法,该方法是将删除元素位置后的所有元素向前移一位,然后将最后一个元素置空
private void fastRemove(Object[] es, int i) {
modCount++;
final int newSize;
if ((newSize = size - 1) > i)
System.arraycopy(es, i + 1, es, i, newSize - i);
es[size = newSize] = null;
}
改
比较简单,直接替换
public E set(int index, E element) {
Objects.checkIndex(index, size);
E oldValue = elementData(index);
elementData[index] = element;
return oldValue;
}
查
比较简单
public E get(int index) {
Objects.checkIndex(index, size);
return elementData(index);
}
Fast-fail机制
多个线程对一个集合进行操作,其中一个线程进行遍历操作,其他线程对集合进行修改,这个时候集合中的modCount字段就会变化,遍历时通过iterator.next()获取元素前会通过checkForComodification()方法检查iterator中记录的modcount同ArrayList中的modCount进行比较,如果不同的话直接抛异常。
final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}