一、ArrayList
ArrayList的继承关系:
变量
- DEFAULT_CAPCITY : 默认容量
- EMPTY_ELEMENTDATA: 空元素数据
- DEFAULTCAPACITY_EMPTY_ELEMENTDATA:默认容量下空元素数据
- elementData: 元素数据素组,用于保存数据
- size: 记录列表大小
/**
* 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.
*/
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 Object[] elementData; // non-private to simplify nested class access
/**
* The size of the ArrayList (the number of elements it contains).
*
* @serial
*/
private int size;
构造函数:
- 默认构造函数:初始化数组大小为10的数组。
- 带初始化数组大小的构造函数:初始化大小为指定大小的数组。
- Copy一个 Collection的List作为一个初始化数据的构造函数,这里有个判断elementData.getClass() != Object[].class,前面的toArray 方法有可能不会返回Object[],如果不是返回的Object[]则使用Arrays.copyOf进行拷贝。
/**
* Constructs an empty list with an initial capacity of ten.
*/
public ArrayList() {
this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
}
/**
* Constructs an empty list with the specified initial capacity.
*
* @param initialCapacity the initial capacity of the list
* @throws IllegalArgumentException if the specified initial capacity
* is negative
*/
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 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;
}
}
自动扩容
arrayList是自动扩容,初始化默认大小为10,在add或者addAll的时候进行自动扩容。
int newCapacity = oldCapacity + (oldCapacity >> 1);扩容的大小为原来的1.5倍,此处的oldCapacity右移动一位 等于 oldCapacity除以2,另外还判断了如果扩容之后的大小没有minCapacity大,则取minCapacity的大小,如果超过了最大大小则取Integer的最大值Integer.MAX_VALUE,最后使用Arrays.copyOf 将原来的复制一份到新的大小数组里面这里Arrays.copyOf 调用的是
System.arraycopy(Object src, //源数组 int srcPos, //源数组要复制的起始位置 Object dest,//目的数组 int destPos,//目的数组放置的起始位置 int length);//复制的长度
这个方法可以复制自身,ArrayList使用到了这个方法实现添加和删除,每次在添加的时候都使用方法ensureCapacityInternal进行扩容。
/**
* Increases the capacity to ensure that it can hold at least the
* number of elements specified by the minimum capacity argument.
*
* @param minCapacity the desired minimum capacity
*/
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);
}
//扩容调用的方法
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);
}