Contents

ArryList实现接口List, RandomAccess, Cloneable, java.io.Serializable
基础的数据结构是数组,根据存储数据的多少动态扩容。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
* 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
/**
* The size of the ArrayList (the number of elements it contains).
*
* @serial
*/
private int size;

elementData是实际存储数据的数组。DEFAULT_CAPACITY是默认的容量,如果没有指定数组的大小。DEFAULTCAPACITY_EMPTY_ELEMENTDATA 与EMPTY_ELEMENTDATA都是 空的初始数组,DEFAULTCAPACITY_EMPTY_ELEMENTDATA是用来判断是不是通过默认方法创建的空数组,而不是删除后才空的。如果指定了数组大小,就用EMPTY_ELEMENTDATA作为默认空数组。

扩容是在添加元素的时候进行,这样不会初始化没有数据的情况下就占据空间。

1
2
3
4
5
6
7
8
9
10
11
/**
* Appends the specified element to the end of this list.
*
* @param e element to be appended to this list
* @return <tt>true</tt> (as specified by {@link Collection#add})
*/
public boolean add(E e) {
ensureCapacityInternal(size + 1); // Increments modCount!!
elementData[size++] = e;
return true;
}

添加方法很简单,先去内部扩容,然后在最后一个添加值,看扩容的内部实现。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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);
}
/**
* 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);
}

ensureCapacityInternal 先是一个判断数组是怎么创建,如果是默认空参数的构造器创建,就使用默认的初始大小10作为最小容量,这样就能避免不能手动创建更小数组的问题。
主要扩容是grow方法,可以看出 newCapacity是扩容0.5倍大小,到原来的1.5倍。
还有就是容器上下限的判断。扩容就是迁移数组newCapacity的大小的数组里。
归根到底,就是将数组扩容1.5倍,旧数组搬迁到新数组。
里面还有几个内部类,一个Itr,实现了Iterator接口,

1
2
3
4
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;

ListItr 实现了 ListIterator,可以前后访问遍历

1
2
3
4
5
private class ListItr extends Itr implements ListIterator<E> {
ListItr(int index) {
super();
cursor = index;
}

ArrayList使用遍历接口,都是使用内部类实现的,重新记录状态,这部分还有些不太懂的,后面继续补充。

Contents