【java基礎】 LinkedList總結

底層實現

LinkedList的繼承關係如下圖所示:
LinkedList的繼承關係

源碼:

public class LinkedList<E> extends AbstractSequentialList<E> implements List<E>, Deque<E>, Cloneable, java.io.Serializable{
    transient int size = 0;

    /**
     * Pointer to first node.
     * Invariant: (first == null && last == null) ||
     *            (first.prev == null && first.item != null)
     */
    transient Node<E> first;

    /**
     * Pointer to last node.
     * Invariant: (first == null && last == null) ||
     *            (last.next == null && last.item != null)
     */
    transient Node<E> last;

    /**
     * Constructs an empty list.
     */
    public LinkedList() {
    }

    /**
     * Constructs a list containing the elements of the specified
     * collection, in the order they are returned by the collection's
     * iterator.
     *
     * @param  c the collection whose elements are to be placed into this list
     * @throws NullPointerException if the specified collection is null
     */
    public LinkedList(Collection<? extends E> c) {
        this();
        addAll(c);
    }
}

LinkedList也是List接口的實現類,而LinkedList數據結構是雙向鏈表。
特點:

  • 實現了List,因此可以進行隊列操作
  • 實現Deque接口,能將LinkedList當做雙端隊列使用
  • 繼承於AbstractSequentialList的雙向鏈表
  • 覆蓋了函數clone(),能被克隆
  • 操作不是線程安全的
    LinkedList繼承了AbstractSequentialList類,實現了List接口,AbstractSequentialList中已經實現了很多方法,如get(int index)、set(int index, E element)、add(int index, E element) 和 remove(int index),這些方法是我們集合操作時使用最多的,不過這些方法在LinkedList中都已經被重寫了,而抽象方法在LinkedList中有了具體實現。

使用指南

屬性

size:集合的長度
first:雙向鏈表頭部節點
last:雙向鏈表尾部節點

方法

給鏈表中添加元素

●boolean add(E e)
在鏈表尾部添加一個元素,如果成功,返回true,否則返回false。

●void add(int index, E element)
在指定位置插入一個元素。

●void addFirst(E e)
在鏈表頭部插入一個元素。

●addLast(E e)
在鏈表尾部添加一個元素。

刪除鏈表中的元素

●boolean remove(Object o)
從當前鏈表中移除指定的元素。

● E remove(int index)
從當前鏈表中移除指定位置的元素。

● E remove()
從當前鏈表中移除第一個元素,同removeLast()相同。

● E removeFirst()
從當前鏈表中移除第一個元素。

● E removeLast()
從當前鏈表中移除最後一個元素。

獲取鏈表中元素

● E get(int index)
從當前鏈表中獲取指定位置的元素。

● E getFirst()
從當前鏈表中獲取第一個元素。

● E getLast()
從當前鏈表中獲取最後一個元素。

關於隊列的操作

隊列:先進先出,後進後出。它只允許在表的前端進行刪除操作,而在表的後端進行插入操作。
LinkedList類實現了Queue接口,因此我們可以把LinkedList當成Queue(隊列)來用。
● E peek()
獲取隊列的第一個元素,如果爲null會返回null

● E element()
獲取隊列的第一個元素,如果爲null會拋出異常

● E poll()
獲取隊列的第一個元素,並在隊列中刪除,如果爲null會返回null

● E remove()
獲取隊列的第一個元素,並在隊列中刪除,如果爲null會拋出異常

● boolean offer(E e)
將元素添加到隊列尾部,並返回是否添加成功

實例代碼:

import java.util.LinkedList;
import java.util.Queue;
 
public class Main {
    public static void main(String[] args) {
        //add()和remove()方法在失敗的時候會拋出異常(不推薦)
        Queue<String> queue = new LinkedList<String>();
        //添加元素
        queue.offer("a");
        queue.offer("b");
        queue.offer("c");
        queue.offer("d");
        queue.offer("e");
        for(String q : queue){
            System.out.println(q);
        }
        System.out.println("===");
        System.out.println("poll="+queue.poll()); //返回第一個元素,並在隊列中刪除
        for(String q : queue){
            System.out.println(q);
        }
        System.out.println("===");
        System.out.println("element="+queue.element()); //返回第一個元素 
        for(String q : queue){
            System.out.println(q);
        }
        System.out.println("===");
        System.out.println("peek="+queue.peek()); //返回第一個元素 
        for(String q : queue){
            System.out.println(q);
        }
    }
}

運行輸出結果爲:

a
b
c
d
e
===
poll=a
b
c
d
e
===
element=b
b
c
d
e
===
peek=b
b
c
d
e

offer,add 區別:
一些隊列有大小限制,因此如果想在一個滿的隊列中加入一個新項,多出的項就會被拒絕。
這時新的 offer 方法就可以起作用了。它不是對調用 add() 方法拋出一個 unchecked 異常,而只是得到由 offer() 返回的 false。

poll,remove 區別:
remove() 和 poll() 方法都是從隊列中刪除第一個元素。remove() 的行爲與 Collection 接口的版本相似, 但是新的 poll() 方法在用空集合調用時不是拋出異常,只是返回 null。因此新的方法更適合容易出現異常條件的情況。

peek,element區別:
element() 和 peek() 用於在隊列的頭部查詢元素。與 remove() 方法類似,在隊列爲空時, element() 拋出一個異常,而 peek() 返回 null。

關於棧的操作

棧:後進先出。所以作爲棧使用也很簡單,添加和刪除元素都只操作隊列的首節點即可。

● addFirst()    //添加元素到列表的起始位置

● addLast()    //添加元素到列表的結束位置

● removeFirst()  //移除列表起始位置的元素

● removeLast()  //移除列表結束位置的元素

● getFirst()    //獲取列表起始位置的元素

● getLast()    //獲取列表結束位置的元素

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章