ArrayList
- 迭代器设计模式
- 数组结构,需要扩容
(1.5倍),迁移元素复制一份
- 线程不安全的
扩容
public boolean add(E e) {
ensureCapacityInternal(size + 1);
elementData[size++] = e;
return true;
}
- ensureCapacityInternal 确保容量
private void ensureCapacityInternal(int minCapacity) {
ensureExplicitCapacity(calculateCapacity(elementData, minCapacity));
}
- calculateCapacity 如果是空的,返回
默认值或者最小值中大的那一个
private static int calculateCapacity(Object[] elementData, int minCapacity) {
if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
return Math.max(DEFAULT_CAPACITY, minCapacity);
}
return minCapacity;
}
- ensureExplicitCapacity 给list扩容
private void ensureExplicitCapacity(int minCapacity) {
modCount++;
if (minCapacity - elementData.length > 0)
grow(minCapacity);
}
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)
//MAX_ARRAR_SIZE 为 Integer.MAX_VALUE - 8
newCapacity = hugeCapacity(minCapacity)
// minCapacity is usually close to size, so this is a win:
//复制一份
elementData = Arrays.copyOf(elementData, newCapacity)
}
remove
public E remove(int index) {
rangeCheck(index);
modCount++;
E oldValue = elementData(index);
int numMoved = size - index - 1;
if (numMoved > 0)
System.arraycopy(elementData, index+1, elementData, index,
numMoved);
elementData[--size] = null;
return oldValue;
}
E elementData(int index) {
return (E) elementData[index];
}
get
public E get(int index) {
rangeCheck(index);
return elementData(index);
}
size
public int size() {
return size;
}
LinkedList
- 双向链表结构,不需要扩容,遍历时
先除2,找到大致位置
- 线程不安全
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
}
}
add
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) //末尾为null,则为插入第一个节点
first = newNode
else //连接
l.next = newNode
size++
modCount++
}
private void linkFirst(E e) {
final Node<E> f = first
final Node<E> newNode = new Node<>(null, e, f)
first = newNode
if (f == null)
last = newNode
else
f.prev = newNode
size++
modCount++
}
offer
public boolean offer(E e) {
return add(e);
}
public boolean offerFirst(E e) {
addFirst(e);
return true;
}
public boolean offerLast(E e) {
addLast(e);
return true;
}
push
public void push(E e) {
addFirst(e);
}
get
public E get(int index) {
checkElementIndex(index);
return node(index).item;
}
Node<E> node(int index) {
if (index < (size >> 1)) { //如果小于size两倍,那么在前半部分
Node<E> x = first
for (int i = 0
x = x.next
return x
} else { //大于size两倍,在后半部分
Node<E> x = last
for (int i = size - 1
x = x.prev
return x
}
}
poll
public E poll() {
final Node<E> f = first;
return (f == null) ? null : unlinkFirst(f);
}
private E unlinkFirst(Node<E> f) {
final E element = f.item
final Node<E> next = f.next
f.item = null
f.next = null
first = next
if (next == null) //维护空的情况
last = null
else
next.prev = null
size--
modCount++
return element
}
pop
public E pop() {
return removeFirst();
}
public E removeFirst() {
final Node<E> f = first;
if (f == null)
throw new NoSuchElementException();
return unlinkFirst(f);
}
remove
public E remove(int index) {
checkElementIndex(index); //越界检查
return unlink(node(index)); //找到响应的node,并移除
}
E unlink(Node<E> x) {
final E element = x.item
final Node<E> next = x.next
final Node<E> prev = x.prev
if (prev == null) { //处理是头的情况
first = next
} else {
prev.next = next
x.prev = null
}
if (next == null) { //处理是尾的情况
last = prev
} else {
next.prev = prev
x.next = null
}
x.item = null
size--
modCount++
return element
}
size
public int size() {
return size;
}