LinkedList與ArrayList同繼承自List接口,其與ArrayList的本質區別便是,ArrayList內置了一個數組,而LinkedList則內置了一個鏈表,其他全部的區別均衍生自二者的本質區別。node
LinkedList與ArrayList的主要區別:數組
經常使用方法:數據結構
內部節點結構app
1 private static class Node<E> { 2 E item; 3 Node<E> next; 4 Node<E> prev; 5 6 Node(Node<E> prev, E element, Node<E> next) { 7 this.item = element; 8 this.next = next; 9 this.prev = prev; 10 } 11 }
數據結構ide
1 transient int size = 0; 2 3 /** 4 * Pointer to first node. 5 * Invariant: (first == null && last == null) || 6 * (first.prev == null && first.item != null) 7 */ 8 transient Node<E> first; 9 10 /** 11 * Pointer to last node. 12 * Invariant: (first == null && last == null) || 13 * (last.next == null && last.item != null) 14 */ 15 transient Node<E> last;
構造方法,LinkedList的默認構造方法爲空,僅僅構建對象。非默認構造方法以下,ui
1 /** 2 * 以參數集合構建新的LinkedList,構建過程調用addAll實現 3 */ 4 public LinkedList(Collection<? extends E> c) { 5 this(); 6 addAll(c); 7 } 8 9 /** 10 * 調用 11 */ 12 public boolean addAll(Collection<? extends E> c) { 13 return addAll(size, c); 14 } 15 16 /** 17 * Inserts all of the elements in the specified collection into this 18 * list, starting at the specified position. Shifts the element 19 * currently at that position (if any) and any subsequent elements to 20 * the right (increases their indices). The new elements will appear 21 * in the list in the order that they are returned by the 22 * specified collection's iterator. 23 * 24 * @param index index at which to insert the first element 25 * from the specified collection 26 * @param c collection containing elements to be added to this list 27 * @return {@code true} if this list changed as a result of the call 28 * @throws IndexOutOfBoundsException {@inheritDoc} 29 * @throws NullPointerException if the specified collection is null 30 */ 31 public boolean addAll(int index, Collection<? extends E> c) { 32 checkPositionIndex(index); 33 34 Object[] a = c.toArray(); 35 int numNew = a.length; 36 if (numNew == 0) 37 return false; 38 39 Node<E> pred, succ; 40 if (index == size) { 41 succ = null; 42 pred = last; 43 } else { 44 succ = node(index); 45 pred = succ.prev; 46 } 47 48 for (Object o : a) { 49 @SuppressWarnings("unchecked") E e = (E) o; 50 Node<E> newNode = new Node<>(pred, e, null); 51 if (pred == null) 52 first = newNode; 53 else 54 pred.next = newNode; 55 pred = newNode; 56 } 57 58 if (succ == null) { 59 last = pred; 60 } else { 61 pred.next = succ; 62 succ.prev = pred; 63 } 64 65 size += numNew; 66 modCount++; 67 return true; 68 }
add(E e)方法,將e插入到鏈表的最尾的一個位置this
1 /** 2 * Appends the specified element to the end of this list. 3 * 4 * <p>This method is equivalent to {@link #addLast}. 5 * 6 * @param e element to be appended to this list 7 * @return {@code true} (as specified by {@link Collection#add}) 8 */ 9 public boolean add(E e) { 10 linkLast(e); 11 return true; 12 } 13 14 /** 15 * 將元素插入到鏈表的尾部 16 */ 17 void linkLast(E e) { 18 final Node<E> l = last; 19 final Node<E> newNode = new Node<>(l, e, null); 20 last = newNode; 21 if (l == null) 22 first = newNode; 23 else 24 l.next = newNode; 25 size++; 26 modCount++; 27 }
add(int index, E e)方法,將e插入到鏈表指定的index位置節點的前面spa
1 /** 2 * Inserts the specified element at the specified position in this list. 3 * Shifts the element currently at that position (if any) and any 4 * subsequent elements to the right (adds one to their indices). 5 * 6 * @param index index at which the specified element is to be inserted 7 * @param element element to be inserted 8 * @throws IndexOutOfBoundsException {@inheritDoc} 9 */ 10 public void add(int index, E element) { 11 checkPositionIndex(index); 12 13 if (index == size) 14 linkLast(element); 15 else 16 linkBefore(element, node(index)); 17 } 18 19 /** 20 * Inserts element e before non-null Node succ. 21 */ 22 void linkBefore(E e, Node<E> succ) { 23 // assert succ != null; 24 final Node<E> pred = succ.prev; 25 final Node<E> newNode = new Node<>(pred, e, succ); 26 succ.prev = newNode; 27 if (pred == null) 28 first = newNode; 29 else 30 pred.next = newNode; 31 size++; 32 modCount++; 33 }
更多的操做均是基於鏈表的形式去操做的,搞清楚LinkedList的數據結構,對LinkedList的經常使用方法也就不陌生了。code