类继承层次
底层数据结构
Object 数组
默认初始化容量
/**
* Default initial capacity.
*/
private static final int DEFAULT_CAPACITY = 10;
最大容量
/**
* The maximum size of array to allocate.
* Some VMs reserve some header words in an array.
* Attempts to allocate larger arrays may result in
* OutOfMemoryError: Requested array size exceeds VM limit
*/
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
扩容算法
/**
* 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);
}
主要操作时间复杂度
add
不扩容的前提下是 O(1)
扩容前提下是O(n)
remove
按索引O(n)
按元素O(n
2
^2
2)
set
只能按索引改O(1)
get
按索引 O(1)
contain,indexOf
O(n)
size,isEmpty
O(1)
安全性
fail-fast
线程不安全