您好,登錄后才能下訂單哦!
LinkedList
LinkedList是一種可以在任何位置進(jìn)行高效地插入和刪除操作的有序序列。
它的最基本存儲(chǔ)結(jié)構(gòu)是一個(gè)節(jié)點(diǎn):每個(gè)節(jié)點(diǎn)將存儲(chǔ)對(duì)象,以及前后節(jié)點(diǎn)的引用。
結(jié)構(gòu)圖
從上面的結(jié)構(gòu)圖中,我們可以了解到 ListedList 底層是基于雙向鏈表實(shí)現(xiàn)的。
圍起來(lái)的可以看成 LinkedList 類,它定義了三個(gè) transient 成員變量:first、last、size。這三個(gè)變量是整個(gè) LinkedList 類的關(guān)鍵點(diǎ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; // 將當(dāng)前最后一個(gè)元素寄存在 l final Node<E> newNode = new Node<>(l, e, null); // new 一個(gè)新節(jié)點(diǎn):pre的引用為l;存儲(chǔ)元素為e;next的引用為null last = newNode; // 將新節(jié)點(diǎn)引用覆蓋成員變量 last if (l == null) first = newNode; // 若l為null,說(shuō)明之前鏈表為空,此時(shí)新節(jié)點(diǎn)為首個(gè)元素 else l.next = newNode; // 否則,更新l的next引用 size++; // size+1 modCount++; // 非查詢操作 modCount 都會(huì) +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é)點(diǎn)前面 } // 檢查 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 距離哪個(gè)鏈表頭近就從哪邊開始開始遍歷 * 時(shí)間復(fù)雜度為 O(n/2); * 當(dāng) index 接近 size 的中間值時(shí),效率最低 * 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)缺點(diǎn)
優(yōu)點(diǎn)
增刪元素效率高(只需要更新節(jié)點(diǎn)附近的引用即可)
缺點(diǎn)
由于查詢需要進(jìn)行遍歷,因此效率低
知識(shí)腦圖
以上所述是小編給大家介紹的Java 集合系列(三)—— LinkedList詳解整合,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)億速云網(wǎng)站的支持!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。