ArrayList
底层是 数组
初始化
1. 不指定容量大小,则 ,用一个空数组,{}
2. 指定容量大小,则,校验容量合理性,创建一个新的Object数组
添加:
1. 如果发生扩容
扩容 原容量的一半 int newCapacity = oldCapacity + (oldCapacity >> 1);
copy: elementData = Arrays.copyOf(elementData, newCapacity);
LinkedList
底层 是 链表
节点
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;
}
}
头尾指针
/**
* Pointer to first node.
* Invariant: (first == null && last == null) ||
* (first.prev == null && first.item != null)
*/
transient Node<E> first;
/**
* Pointer to last node.
* Invariant: (first == null && last == null) ||
* (last.next == null && last.item != null)
*/
transient Node<E> last;
以上可得知,LinkedList 是双向链表