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 是双向链表

本文详细探讨了ArrayList和LinkedList两种Java集合类的底层实现。ArrayList基于数组,初始为空或按指定容量创建。添加元素时,若需扩容,会扩大原容量的一半并进行复制。LinkedList作为双向链表,其节点包含前后指针,便于高效插入和删除操作。总结了两者在内存使用和性能上的区别。
2220

被折叠的 条评论
为什么被折叠?



