ArryList是一个依靠数组存储元素的类
继承和实现关系:
ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, java.io.Serializable
关键属性:
* Default initial capacity.
* 默认初始容量
*/
private static final int DEFAULT_CAPACITY = 10;
/**
* Shared empty array instance used for empty instances.
* 用于空实例的共享空数组实例。
*/
private static final Object[] EMPTY_ELEMENTDATA = {};
/**
* Shared empty array instance used for default sized empty instances. We
* distinguish this from EMPTY_ELEMENTDATA to know how much to inflate when
* first element is added.
* 用于默认大小的空实例的共享空数组实例。
* 我们将其与EMPTY_ELEMENTDATA区分开来,以了解添加第一个元素时扩容多少。
*/
private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
/**
* The array buffer into which the elements of the ArrayList are stored.
* The capacity of the ArrayList is the length of this array buffer. Any
* empty ArrayList with elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA
* will be expanded to DEFAULT_CAPACITY when the first element is added.
* 存储实际元素的数组,transient 修饰的属性不能被序列化
*/
transient Object[] elementData; // non-private to simplify nested class access
/**
* The size of the ArrayList (the number of elements it contains).
* 包含的元素个数
* @serial
*/
private int size;
提炼出来就是下面五个:
1.DEFAULT_CAPACITY = 10;
2.EMPTY_ELEMENTDATA = {};
3.DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
4.elementData;
5.size;
构造方法:
1.无参构造方法
public ArrayList() {
this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
}
2.指定初始容量的有参构造方法
if (initialCapacity > 0) {
this.elementData = new Object[initialCapacity];
} else if (initialCapacity == 0) {
this.elementData = EMPTY_ELEMENTDATA;
} else {
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
}
}
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;
}
}
添加元素和扩容机制:
// 关键方法
ensureCapacityInternal(size + 1); // Increments modCount!!
elementData[size++] = e;
return true;
}
ensureCapacityInternal方法涉及到了容量判断和扩容的实现
ensureExplicitCapacity(calculateCapacity(elementData, minCapacity));
}
calculateCapacity方法判断和计算容量,看下面代码可知道,如果是空数组,则选择DEFAULT_CAPACITY(默认容量=10)和 minCapacity之间大的那一个,非空数组直接返回minCapacity
private static int calculateCapacity(Object[] elementData, int minCapacity) {
if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
return Math.max(DEFAULT_CAPACITY, minCapacity);
}
return minCapacity;
}
再看ensureExplicitCapacity方法,有段判断逻辑: minCapacity - elementData.length > 0 ,即元素个数要超过容量的时候就会执行具体的扩容方法:grow方法
// modCount记录list结构被修改的次数,添加删除都会加一,但是修改不会
modCount++;
// overflow-conscious code
if (minCapacity - elementData.length > 0)
grow(minCapacity);
}
grow方法具体实现
// overflow-conscious code
// 获取旧的容量
int oldCapacity = elementData.length;
// 新的容量等于1.5倍旧容量(向下取整)
int newCapacity = oldCapacity + (oldCapacity >> 1);
// 如果扩大之后的容量不够,则新的容量等于minCapacity
if (newCapacity - minCapacity < 0)
newCapacity = minCapacity;
// 如果新的容量超过了MAX_ARRAY_SIZE(2的31次方减一,再减8),则调用hugeCapacity方法获取大容量
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);
}
hugeCapacity方法获取最大容量
if (minCapacity < 0) // overflow
throw new OutOfMemoryError();
return (minCapacity > MAX_ARRAY_SIZE) ?
Integer.MAX_VALUE :
MAX_ARRAY_SIZE;
}
Arrays.copyOf方法底层调用的是一个native方法:System.arraycopy方法
Object dest, int destPos,
int length);