1. 程式人生 > >ArrayList和LinkedList原始碼中的重點關注點

ArrayList和LinkedList原始碼中的重點關注點

1.ArrayList(最重要的操作是陣列複製,擴容)

 通過Object[]來儲存資料;

transient Object[] elementData;

1)構造器,引數Collection,直接把Collection轉成Array

public ArrayList(Collection<? extends E> c) {
elementData = c.toArray();
if ((size = elementData.length) != 0) {
// c.toArray might (incorrectly) not return Object[] (see 6260652)
if (elementData.getClass() != Object[].class)
elementData = Arrays.copyOf(elementData, size, Object[].class);
} else {
// replace with empty array.
this.elementData = EMPTY_ELEMENTDATA;
}
}
2)擴容,如何保證oldCapacity != 0和newCapacity不大於Integer.MAX_VALUE
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);
}
3)克隆是淺克隆,引用的複製
public Object clone() {
try {
ArrayList<?> v = (ArrayList<?>) super.clone();
v.elementData = Arrays.copyOf(elementData, size);
v.modCount = 0;
return v;
} catch (CloneNotSupportedException e) {
// this shouldn't happen, since we are Cloneable
throw new InternalError(e);
}
}
4)subList,新建一個與此List內部相關的內部類SubList

2.LinkedList(雙向連結串列實現,支援雙端操作,可以用作佇列,棧,注意點是:連結串列節點的新增和釋放)
通過Node儲存資料,資料區儲存了first,last,用於操作連結串列
transient Node<E> first;
transient Node<E> last;
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(index),查詢位於某個位置的元素

Node<E> node(int index) {
    // assert isElementIndex(index);

if (index < (size >> 1)) {
Node<E> x = first;
for (int i = 0; i < index; i++)
x = x.next;
return x;
} else {
Node<E> x = last;
for (int i = size - 1; i > index; i--)
x = x.prev;
return x;
}
}