LinkedList源码详解
生活随笔
收集整理的這篇文章主要介紹了
LinkedList源码详解
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
public class LinkedList<E>extends AbstractSequentialList<E>implements List<E>, Deque<E>, Cloneable, java.io.Serializable
LinkedList繼承自AbstractSequenceList、實現了List及Deque接口。
AbstractSequenceList提供List接口的骨干實現,以減少實現List接口的復雜度。
Deque是Queue的一個子接口,雙向隊列是指該隊列兩端的元素既能入隊(offer)也能出隊(poll)。
- LinkedList的屬性
- LinkedList的構造方法
- LinkedList的方法
add方法
remove方法
public boolean remove(Object o) { //判斷要刪除的元素是不是nullif (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;}public E remove(int index) {checkElementIndex(index); //檢查數組下標越界return unlink(node(index));}private void checkElementIndex(int index) {if (!isElementIndex(index))throw new IndexOutOfBoundsException(outOfBoundsMsg(index));}private boolean isElementIndex(int index) {return index >= 0 && index < size;}Node<E> node(int index) {// assert isElementIndex(index);if (index < (size >> 1)) { //假如index小于size的一半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;}}E unlink(Node<E> x) {// assert x != null; //假如LinkerList有這么一組數據[1,2,3,4,5] remove index是0的數據final E element = x.item; //則節點是1 x.item = 1 next = 2 prev = 3 final Node<E> next = x.next;final Node<E> prev = x.prev;if (prev == null) { //prev ==null,2現在就是首節點了first = next; //這里要注意一點,假如有這么一組數據[1,2,3,null,null,null,8,9],remove index是4} else { //item prve next 分別為空 ,只是表示指向的內容是nullprev.next = next; //假如刪除的不是頭結點,將下個節點指向上個節點的下個節點x.prev = null; //將刪除的節點的上個節點指向設置為null}if (next == null) { last = prev;} else { //接著[1,2,3,4,5]來解釋next.prev = prev; //假如刪除的不是尾結點,prve此時是null了,2是首節點,2.prev = null;x.next = null; //將刪除的節點的下個節點指向設置為null}x.item = null; //將刪除的節點也置為nullsize--;modCount++;return element;}set
public E set(int index, E element) {checkElementIndex(index); //檢查數據下標越界Node<E> x = node(index); //獲取要修改坐標的元素E oldVal = x.item; //獲取這個坐標元素的item屬性x.item = element; //將要修改元素的item屬性引用指向elementreturn oldVal;}get
public E get(int index) {checkElementIndex(index); //檢查數據下標越界return node(index).item; //for循環來取}Iterator
private class ListItr implements ListIterator<E> { //正序迭代private Node<E> lastReturned = null;private Node<E> next;private int nextIndex;private int expectedModCount = modCount;ListItr(int index) {next = (index == size) ? null : node(index);nextIndex = index;}public boolean hasNext() {return nextIndex < size;}public E next() {checkForComodification();if (!hasNext())throw new NoSuchElementException();lastReturned = next;next = next.next;nextIndex++;return lastReturned.item;}public boolean hasPrevious() {return nextIndex > 0;}public E previous() {checkForComodification();if (!hasPrevious())throw new NoSuchElementException();lastReturned = next = (next == null) ? last : next.prev;nextIndex--;return lastReturned.item;}public int nextIndex() {return nextIndex;}public int previousIndex() {return nextIndex - 1;}public void remove() {checkForComodification();if (lastReturned == null)throw new IllegalStateException();Node<E> lastNext = lastReturned.next;unlink(lastReturned);if (next == lastReturned)next = lastNext;elsenextIndex--;lastReturned = null;expectedModCount++;}public void set(E e) {if (lastReturned == null)throw new IllegalStateException();checkForComodification();lastReturned.item = e;}public void add(E e) {checkForComodification();lastReturned = null;if (next == null)linkLast(e);elselinkBefore(e, next);nextIndex++;expectedModCount++;}final void checkForComodification() {if (modCount != expectedModCount)throw new ConcurrentModificationException();}}private class DescendingIterator implements Iterator<E> { //倒序迭代private final ListItr itr = new ListItr(size());public boolean hasNext() {return itr.hasPrevious();}public E next() {return itr.previous();}public void remove() {itr.remove();}}總結
linkedList基于雙向鏈表實現,有序,允許為null,沒有固定容量,不需要擴容。
其中的每個對象包含數據的同時還包含指向鏈表中前一個與后一個元素的引用。
轉載于:https://www.cnblogs.com/Ch1nYK/p/8589415.html
總結
以上是生活随笔為你收集整理的LinkedList源码详解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [COCI2017-2018#1] Pl
- 下一篇: 浅谈WM算法