源自:jdk1.8.0_121
ArrayList
继承自AbstractList
,实现了List
、RandomAccess
、Cloneable
、Serializable
。
变量
// 默认容量为10
private static final int DEFAULT_CAPACITY = 10;
// 空数组
private static final Object[] EMPTY_ELEMENTDATA = {};
// 默认空数组
private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
// 存放数据的数组,被transient修饰的参数不会被序列化
transient Object[] elementData;
// 实际元素的大小
private int 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) {
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;
}
}
toArray()实现方式的不同
注:
ArrayList(Collection<? extends E> c)
之所以要判断是否为Object类型,是因为调用toArray()
方法的实现方式不同。
在java.util.ArrayList
中,toArray()
返回的是Object数组。
public Object[] toArray() {
return Arrays.copyOf(elementData, size);
}
在java.util.Arrays
中,有个名为ArrayList
的内部类,当调用Arrays.asList()
时,返回的是java.util.Arrays$ArrayList
内部类对象,而并非java.util.ArrayList
。java.util.Arrays$ArrayList
中的toArray()
返回的虽然是Object数组,但是它的真实类型是个泛型数组。
private final E[] a;
public Object[] toArray() {
return a.clone();
}
ArrayList扩容
public boolean add(E e) {
ensureCapacityInternal(size + 1); // Increments modCount!!
elementData[size++] = e;
return true;
}
private void ensureCapacityInternal(int minCapacity) {
if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
}
ensureExplicitCapacity(minCapacity);
}
private void ensureExplicitCapacity(int minCapacity) {
modCount++;
// overflow-conscious code
if (minCapacity - elementData.length > 0)
grow(minCapacity);
}
private void grow(int minCapacity) {
// overflow-conscious code
int oldCapacity = elementData.length;
int newCapacity = oldCapacity + (oldCapacity >> 1);
if (newCapacity - minCapacity < 0)
newCapacity = minCapacity;
if (newCapacity - MAX_ARRAY_SIZE > 0)
newCapacity = hugeCapacity(minCapacity);
// minCapacity is usually close to size, so this is a win:
elementData = Arrays.copyOf(elementData, newCapacity);
}
从源码可以看出,如果1.5倍的elementData.length
小于10,会将elementData
的大小扩容成默认的10,反之,则会以1.5倍的elementData.length
进行扩容。
将elementData设置为实际容量,动态扩充的多余容量将被删除
public void trimToSize() {
modCount++;
if (size < elementData.length) {
elementData = (size == 0)
? EMPTY_ELEMENTDATA
: Arrays.copyOf(elementData, size);
}
}