RandomAccess
接口支持随机访问,非线程安全类型Object[] elementData;
public ArrayList() {
this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
}
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 boolean add(E e) {
ensureCapacityInternal(size + 1);
// 将元素e添加到末尾
elementData[size++] = e;
return true;
}
private static int calculateCapacity(Object[] elementData, int minCapacity) {
// 判断是否是为空数组,若为空,返回10;否则将返回arr.length + 1(第一次添加数据肯定是空的,第二次及后续就是arr.length +1)
if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
return Math.max(DEFAULT_CAPACITY, minCapacity);
}
return minCapacity;
}
private void ensureExplicitCapacity(int minCapacity) {
if (minCapacity - elementData.length > 0)
grow(minCapacity);
}
// 核心 开始扩容
private void grow(int minCapacity) {
// 获取数组长度
int oldCapacity = elementData.length;
// 右移位运算得出扩容后的长度
int newCapacity = oldCapacity + (oldCapacity >> 1);
if (newCapacity - minCapacity < 0)
newCapacity = minCapacity;
if (newCapacity - MAX_ARRAY_SIZE > 0)
newCapacity = hugeCapacity(minCapacity);
// 将扩容后的数据赋值给原数组
elementData = Arrays.copyOf(elementData, newCapacity);
}
在添加第一个元素时,就会调用grow()
方法进行扩容操作,但此时数组长度为0,扩容后的
Deque
接口,可以双向操作,不支持随机访问,非线程安全类型 // node节点
private static class Node<E> {
E item;
Node<E> next;
Node<E> prev;
Node(Node<E> prev, E element, Node<E> next) {
this.item = element;
this.next = next;
this.prev = prev;
}
}
// 指向上一个节点
Node<E> first;
// 指向下一个节点
Node<E> last;
public LinkedList() {
}
public LinkedList(Collection<? extends E> c) {
this();
addAll(c);
}sdss
public boolean add(E e) {
linkLast(e);
return true;
}
// 这个就不解释,应该懂得
void linkLast(E e) {
final Node<E> l = last;
final Node<E> newNode = new Node<>(l, e, null);
last = newNode;
if (l == null)
first = newNode;
else
l.next = newNode;
size++;
modCount++;
}
RandomAccess
接口,支持随机访问,线程安全类型protected Object[] elementData;
public Vector(int initialCapacity, int capacityIncrement) {
super();
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
this.elementData = new Object[initialCapacity];
this.capacityIncrement = capacityIncrement;
}
public Vector(int initialCapacity) {
this(initialCapacity, 0);
}
public Vector() {
this(10);
}
public synchronized void addElement(E obj) {
// 扩容
ensureCapacityHelper(elementCount + 1);
// 将新增元素添加到数组末尾
elementData[elementCount++] = obj;
}
private void ensureCapacityHelper(int minCapacity) {
// 第一次添加元素时,minCapacity=10,elementData.length = 0,这时不会进行扩容
if (minCapacity - elementData.length > 0)
grow(minCapacity);
}
private void grow(int minCapacity) {
// 获取数据的长度
int oldCapacity = elementData.length;
// 扩容核心:在创建数组时指定了capacityIncrement且大于0,扩容后的值为capacityIncrement+初始容量,反之为初始容量+初始容量,即两倍的初始容量
int newCapacity = oldCapacity + ((capacityIncrement > 0) ?
capacityIncrement : oldCapacity);
if (newCapacity - minCapacity < 0)
newCapacity = minCapacity;
if (newCapacity - MAX_ARRAY_SIZE > 0)
newCapacity = hugeCapacity(minCapacity);
elementData = Arrays.copyOf(elementData, newCapacity);
}
int newCapacity = oldCapacity + (oldCapacity >> 1);
即 10 --> 15;int newCapacity = oldCapacity + ((capacityIncrement > 0) ? capacityIncrement : oldCapacity);
,即:10 --> 20网站原文:https://www.haicheng.website/passages/source-code-analysis-01/