LinkedList
創(chuàng)新互聯(lián)建站基于成都重慶香港及美國等地區(qū)分布式IDC機房數(shù)據(jù)中心構(gòu)建的電信大帶寬,聯(lián)通大帶寬,移動大帶寬,多線BGP大帶寬租用,是為眾多客戶提供專業(yè)服務器托管報價,主機托管價格性價比高,為金融證券行業(yè)鄭州服務器托管,ai人工智能服務器托管提供bgp線路100M獨享,G口帶寬及機柜租用的專業(yè)成都idc公司。
LinkedList是一種可以在任何位置進行高效地插入和刪除操作的有序序列。
它的最基本存儲結(jié)構(gòu)是一個節(jié)點:每個節(jié)點將存儲對象,以及前后節(jié)點的引用。
結(jié)構(gòu)圖

從上面的結(jié)構(gòu)圖中,我們可以了解到 ListedList 底層是基于雙向鏈表實現(xiàn)的。
圍起來的可以看成 LinkedList 類,它定義了三個 transient 成員變量:first、last、size。這三個變量是整個 LinkedList 類的關(guān)鍵點。
源碼分析
add(E e) 源碼分析
/**
* Appends the specified element to the end of this list.
*
* <p>This method is equivalent to {@link #addLast}.
*
* @param e element to be appended to this list
* @return {@code true} (as specified by {@link Collection#add})
*/
public boolean add(E e) {
linkLast(e);
return true;
}
/**
* Links e as last element.
*/
void linkLast(E e) {
final Node<E> l = last; // 將當前最后一個元素寄存在 l
final Node<E> newNode = new Node<>(l, e, null); // new 一個新節(jié)點:pre的引用為l;存儲元素為e;next的引用為null
last = newNode; // 將新節(jié)點引用覆蓋成員變量 last
if (l == null)
first = newNode; // 若l為null,說明之前鏈表為空,此時新節(jié)點為首個元素
else
l.next = newNode; // 否則,更新l的next引用
size++; // size+1
modCount++; // 非查詢操作 modCount 都會 +1
}add(int index, E element) 方法分析
/**
* Inserts the specified element at the specified position in this list.
* Shifts the element currently at that position (if any) and any
* subsequent elements to the right (adds one to their indices).
*
* @param index index at which the specified element is to be inserted
* @param element element to be inserted
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
public void add(int index, E element) {
checkPositionIndex(index); // 檢查 index 是否大于 size
if (index == size)
linkLast(element); // 直接在鏈表末尾追加
else
linkBefore(element, node(index)); // 插入index 節(jié)點前面
}
// 檢查 index 是否超出范圍 超出則拋出 IndexOutOfBoundsException
private void checkPositionIndex(int index) {
if (!isPositionIndex(index))
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
/**
* Tells if the argument is the index of a valid position for an
* iterator or an add operation.
*/
private boolean isPositionIndex(int index) {
return index >= 0 && index <= size;
}
/**
* 根據(jù) index 查找 node
* 該方法利用了雙向鏈表的特性,index 距離哪個鏈表頭近就從哪邊開始開始遍歷
* 時間復雜度為 O(n/2);
* 當 index 接近 size 的中間值時,效率最低
* Returns the (non-null) Node at the specified element index.
*/
Node<E> node(int index) {
// assert isElementIndex(index);
if (index < (size >> 1)) { // size 右移一位(除以2)
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;
}
}優(yōu)缺點
優(yōu)點
增刪元素效率高(只需要更新節(jié)點附近的引用即可)
缺點
由于查詢需要進行遍歷,因此效率低
知識腦圖

以上所述是小編給大家介紹的Java 集合系列(三)—— LinkedList詳解整合,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對創(chuàng)新互聯(lián)網(wǎng)站的支持!
網(wǎng)頁名稱:詳解Java集合系列(三)——LinkedList
地址分享:http://www.chinadenli.net/article36/iiessg.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供面包屑導航、虛擬主機、用戶體驗、手機網(wǎng)站建設、標簽優(yōu)化、關(guān)鍵詞優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)