Java集合 LinkedList原始碼理解筆記

2021-09-25 00:00:26 字數 1696 閱讀 7651

1.內部類node節點,item儲存資料內容,next、prev儲存前後節點,形成鍊錶。

private static class node

}

2.first、last屬性字段儲存開始節點和結束節點。

/**

* pointer to first node.

* invariant: (first == null && last == null) ||

* (first.prev == null && first.item != null)

*/transient nodefirst;

/** * pointer to last node.

* invariant: (first == null && last == null) ||

* (last.next == null && last.item != null)

*/transient nodelast;

3.push(e e)將鍊錶當成棧使用,在鍊錶第乙個元素前新增新元素,入棧;e pop()返回第乙個元素,出棧,當沒有元素時會拋異常。

4.offer(e e) peek() poll()等queue佇列方法與list方法的對比,主要一點是佇列不會丟擲異常,而是返回null

方法作用

原有返回值

佇列返回值

對比新增元素

add(e e)

成功返回true

offer(e e)

成功返回true

直接呼叫前者

新增元素

addfirst(e e)

無offerfirst(e e)

成功返回true

直接呼叫前者,加返回值

新增元素

addlast(e e)

無offerlast(e e)

成功返回true

直接呼叫前者,加返回值

返回首元素

e peek()

沒有元素返回null

返回首元素

e element()

沒有元素丟擲異常

返回首元素

e peekfirst()

沒有元素返回null

返回尾元素

e peeklast()

沒有元素返回null

刪除首元素

e remove()

返回被刪除的元素,沒有元素丟擲異常

e poll()

返回被刪除的元素,沒有元素返回null

刪除首元素

e removefirst()

返回被刪除的元素,沒有元素丟擲異常

e pollfirst()

返回被刪除的元素,沒有元素返回null

刪除尾元素

e removelast()

返回被刪除的元素,沒有元素丟擲異常

e polllast()

返回被刪除的元素,沒有元素返回null

刪除尾元素

e removelast()

返回被刪除的元素,沒有元素丟擲異常

e polllast()

返回被刪除的元素,沒有元素返回null

5.有序列化與反序列化writeobject、readobject方法

總結:雙向鍊錶,相較於arraylist易於插入與刪除

java原始碼分析08 LinkedList

喜歡乙個人,就會喜歡她的一切嗎?今天,我們來看下linkedlist的結構。linkedlist內部其實是雙向鍊錶實現的,而且擁有輪詢以及出棧的功能。增刪改查 public boolean add e e void linklast e e 每次增加新的元素,都是在鍊錶末尾加上,注意當前時刻的末尾節...

Java資料結構詳解(四) LinkedList

返回鍊錶的邏輯大小 transient int size 0 頭部節點 transient nodefirst 尾部節點 transient nodelast 一,無參構造器 public linkedlist 二,有引數的構造器 public linkedlist collection c add...

Java資料結構之手寫LinkedList

linkedlist的實現本質為乙個雙向鍊錶,下面是簡單的實現增刪改查 public class linkedlist public void add e e public void change int index,e e nodetarget node index target.item e 刪...