1 linkedList 的定義
public class LinkedList<E>
extends AbstractSequentialList<E>
implements List<E>, Deque<E>, Cloneable, java.io.Serializable
從這段代碼中咱們能夠清晰地看出LinkedList繼承AbstractSequentialList,實現List、Deque、Cloneable、Serializable。其中AbstractSequentialList提供了 List 接口的骨幹實現,從而最大限度地減小了實現受「連續訪問」數據存儲(如連接列表)支持的此接口所需的工做,從而以減小實現List接口的複雜度。Deque一個線性 collection,支持在兩端插入和移除元素,定義了雙端隊列的操做。
從中能夠看出LinkedList既實現了List接口也實現了雙向隊列的接口
二、屬性
transient int size = 0;
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;
}
}
size 記錄鏈表中的節點個數,LinkedList是個雙向鏈表因此有先後兩個節點,在LinkedList裏有個內部類即節點類node
三、構造方法
public LinkedList() {
}
默認的構造方法內不作任何操做
public LinkedList(Collection<? extends E> c) {
this();
addAll(c);
}
public boolean addAll(Collection<? extends E> c) {
return addAll(size, c);
}
public boolean addAll(int index, Collection<? extends E> c) {
checkPositionIndex(index);
Object[] a = c.toArray();
int numNew = a.length;
if (numNew == 0)
return false;
Node<E> pred, succ;
if (index == size) {
succ = null;
pred = last;
} else {
succ = node(index);
pred = succ.prev;
}
for (Object o : a) {
@SuppressWarnings("unchecked") E e = (E) o;
Node<E> newNode = new Node<>(pred, e, null);
if (pred == null)
first = newNode;
else
pred.next = newNode;
pred = newNode;
}
if (succ == null) {
last = pred;
} else {
pred.next = succ;
succ.prev = pred;
}
size += numNew;
modCount++;
return true;
}
參數是集合的構造函數,他調用了addAll(c) 方法,addAll(c)中有調用了addAll(size, c)方法,size表明的是插入的下標位置,而後爲每一個對象一個一個地創造node類,而後進行鏈表插入操做,addAll()方法中調用了node(index)函數,該函數是查找到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;
}
}
該函數的思想是判斷index在前半段仍是在後半段,若在前半段用first遍歷 若在後半段用last遍歷
四、增長方法
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++;
}
直接調用add()方法參數是一個對象,其實是在鏈表最後加上新的節點,讓last指向新的節點
public void add(int index, E element) {
checkPositionIndex(index);
if (index == size)
linkLast(element);
else
linkBefore(element, node(index));
}
void linkBefore(E e, Node<E> succ) {
// assert succ != null;
final Node<E> pred = succ.prev;
final Node<E> newNode = new Node<>(pred, e, succ);
succ.prev = newNode;
if (pred == null)
first = newNode;
else
pred.next = newNode;
size++;
modCount++;
}
在指定下標位置插入新的對象,先判斷index是否有效,有效位置爲0-size , 若是爲size 直接調用add()函數加到鏈表尾,不然調用 linkBefore()函數
其餘的還有
public boolean addAll(Collection<? extends E> c) {
return addAll(size, c);
}
////////////////////////////////////////往鏈表前加入新的對象
public void addFirst(E e) {
linkFirst(e);
}
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++;
}
//////////////////////////////////////////////////////////////////////////////////////////
在鏈表後加入新的節點
public void addLast(E e) {
linkLast(e);
}
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++;
}
五、刪除方法
public boolean remove(Object o) {
if (o == null) {
for (Node<E> x = first; x != null; x = x.next) {
if (x.item == null) {
unlink(x);
return true;
}
}
} else {
for (Node<E> x = first; x != null; x = x.next) {
if (o.equals(x.item)) {
unlink(x);
return true;
}
}
}
return false;
}
E unlink(Node<E> x) {
// assert x != null;
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;
}
該方法是刪除鏈表中的某個對象,其實是個遍歷鏈表比較的一個過程,分兩種狀況,刪除的對象是否爲null,刪除的是最小index符合條件的對象並非刪除鏈表中知足條件的全部對象
public E remove(int index) {
checkElementIndex(index);
return unlink(node(index));
}
刪除在index位置上的對象
其餘方法還有:
removeFirst() 刪除頭結點
removeLast() 刪除末尾節點
removeFirstOccurrence(Object o) 刪除從頭至尾遍歷是o的第一個對象 實際上調用的是remove(Object o) 二者是一個概念
removeLastOccurrence(Object o) 刪除從尾到頭遍歷是o的第一個對象
六、查找方法
public E get(int index) {
checkElementIndex(index);
return node(index).item;
}
返回下標位置爲index的對象內容
public E getFirst() {
final Node<E> f = first;
if (f == null)
throw new NoSuchElementException();
return f.item;
}
返回頭結點內容
public E getLast() {
final Node<E> l = last;
if (l == null)
throw new NoSuchElementException();
return l.item;
}
返回尾節點的內容
7序列化與反序列化
從定義中LinkedList實現了serializable 因此能夠進行序列化 ,在源碼中有對序列化和反序列化的函數進行重寫
private void writeObject(java.io.ObjectOutputStream s)
throws java.io.IOException {
// Write out any hidden serialization magic
s.defaultWriteObject();
// Write out size
s.writeInt(size);
// Write out all elements in the proper order.
for (Node<E> x = first; x != null; x = x.next)
s.writeObject(x.item);
}
private void readObject(java.io.ObjectInputStream s)
throws java.io.IOException, ClassNotFoundException {
// Read in any hidden serialization magic
s.defaultReadObject();
// Read in size
int size = s.readInt();
// Read in all elements in the proper order.
for (int i = 0; i < size; i++)
linkLast((E)s.readObject());
}
}