java中LinkedList有什么用

這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)java中LinkedList有什么用,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

創(chuàng)新互聯(lián)"三網(wǎng)合一"的企業(yè)建站思路。企業(yè)可建設(shè)擁有電腦版、微信版、手機版的企業(yè)網(wǎng)站。實現(xiàn)跨屏營銷,產(chǎn)品發(fā)布一步更新,電腦網(wǎng)絡(luò)+移動網(wǎng)絡(luò)一網(wǎng)打盡,滿足企業(yè)的營銷需求!創(chuàng)新互聯(lián)具備承接各種類型的成都網(wǎng)站設(shè)計、成都做網(wǎng)站項目的能力。經(jīng)過10年的努力的開拓,為不同行業(yè)的企事業(yè)單位提供了優(yōu)質(zhì)的服務(wù),并獲得了客戶的一致好評。

整體架構(gòu)

底層數(shù)據(jù)結(jié)構(gòu)是一個雙向鏈表、適用于有順序迭代的場景

java中LinkedList有什么用

first:頭節(jié)點
last:尾節(jié)點
當(dāng)鏈表為空時,last、first都是同一個節(jié)點,前后都指向null
因為是雙向鏈表,只要機器內(nèi)存足夠強大,是沒有大小限制的
node有是三個屬性:prev、next、item

private static class Node<E> {
    E item;// 節(jié)點值
    Node<E> next; // 指向的下一個節(jié)點
    Node<E> prev; // 指向的前一個節(jié)點

    // 初始化參數(shù)順序分別是:前一個節(jié)點、本身節(jié)點值、后一個節(jié)點
    Node(Node<E> prev, E element, Node<E> next) {
        this.item = element;
        this.next = next;
        this.prev = prev;
    }
}

源碼解析

一、新增、刪除

頭部新增:addFirst

// 從頭部追加
private void linkFirst(E e) {
    // 頭節(jié)點賦值給臨時變量
    final Node<E> f = first;
    // 新建節(jié)點,前一個節(jié)點指向null,e 是新建節(jié)點,f 是新建節(jié)點的下一個節(jié)點,目前值是頭節(jié)點的值
    final Node<E> newNode = new Node<>(null, e, f);
    // 新建節(jié)點成為頭節(jié)點
    first = newNode;
    // 頭節(jié)點為空,就是鏈表為空,頭尾節(jié)點是一個節(jié)點
    if (f == null)
        last = newNode;
    //上一個頭節(jié)點的前一個節(jié)點指向當(dāng)前節(jié)點
    else
        f.prev = newNode;
    size++;
    modCount++;
}

尾部新增:add

// 從尾部開始追加節(jié)點
void linkLast(E e) {
    // 把尾節(jié)點數(shù)據(jù)暫存
    final Node<E> l = last;
    // 新建新的節(jié)點,初始化入?yún)⒑x:
    // l 是新節(jié)點的前一個節(jié)點,當(dāng)前值是尾節(jié)點值
    // e 表示當(dāng)前新增節(jié)點,當(dāng)前新增節(jié)點后一個節(jié)點是 null
    final Node<E> newNode = new Node<>(l, e, null);
    // 新建節(jié)點追加到尾部
    last = newNode;
    //如果鏈表為空(l 是尾節(jié)點,尾節(jié)點為空,鏈表即空),頭部和尾部是同一個節(jié)點,都是新建的節(jié)點
    if (l == null)
        first = newNode;![圖片描述](//img.mukewang.com/5d5fc69600013e4803600240.gif)
    //否則把前尾節(jié)點的下一個節(jié)點,指向當(dāng)前尾節(jié)點。
    else
        l.next = newNode;
    //大小和版本更改
    size++;
    modCount++;
}

java中LinkedList有什么用

刪除
與新增類似、可以頭部刪除unlinkFirst、可以尾部刪除unlinkLast

//從頭刪除節(jié)點 f 是鏈表頭節(jié)點
private E unlinkFirst(Node<E> f) {
    // 拿出頭節(jié)點的值,作為方法的返回值
    final E element = f.item;
    // 拿出頭節(jié)點的下一個節(jié)點
    final Node<E> next = f.next;
    //幫助 GC 回收頭節(jié)點    f.item = null;
    f.next = null;
    // 頭節(jié)點的下一個節(jié)點成為頭節(jié)點
    first = next;
    //如果 next 為空,表明鏈表為空
    if (next == null)
        last = null;
    //鏈表不為空,頭節(jié)點的前一個節(jié)點指向 null
    else
        next.prev = null;
    //修改鏈表大小和版本
    size--;
    modCount++;
    return element;
}

查詢
LinkedList采用二分法進(jìn)行循環(huán)查詢(平時可借鑒)

// 根據(jù)鏈表索引位置查詢節(jié)點
Node<E> node(int index) {
    // 如果 index 處于隊列的前半部分,從頭開始找,size >> 1 是 size 除以 2 的意思。
    if (index < (size >> 1)) {
        Node<E> x = first;
        // 直到 for 循環(huán)到 index 的前一個 node 停止
        for (int i = 0; i < index; i++)
            x = x.next;
        return x;
    } else {// 如果 index 處于隊列的后半部分,從尾開始找
        Node<E> x = last;
        // 直到 for 循環(huán)到 index 的后一個 node 停止
        for (int i = size - 1; i > index; i--)
            x = x.prev;
        return x;
    }
}

二、迭代器

ListIterator:提供了向前、向后的迭代方式

// 雙向迭代器
private class ListItr implements ListIterator<E> {
    private Node<E> lastReturned;//上一次執(zhí)行 next() 或者 previos() 方法時的節(jié)點位置
    private Node<E> next;//下一個節(jié)點
    private int nextIndex;//下一個節(jié)點的位置
    //expectedModCount:期望版本號;modCount:目前最新版本號
    private int expectedModCount = modCount;
    …………
}

從頭到尾:

// 判斷還有沒有下一個元素
public boolean hasNext() {
    return nextIndex < size;// 下一個節(jié)點的索引小于鏈表的大小,就有
}

// 取下一個元素
public E next() {
    //檢查期望版本號有無發(fā)生變化
    checkForComodification();
    if (!hasNext())//再次檢查
        throw new NoSuchElementException();
    // next 是當(dāng)前節(jié)點,在上一次執(zhí)行 next() 方法時被賦值的。
    // 第一次執(zhí)行時,是在初始化迭代器的時候,next 被賦值的
    lastReturned = next;
    // next 是下一個節(jié)點了,為下次迭代做準(zhǔn)備
    next = next.next;
    nextIndex++;
    return lastReturned.item;
}

從尾到頭:

// 如果上次節(jié)點索引位置大于 0,就還有節(jié)點可以迭代
public boolean hasPrevious() {
    return nextIndex > 0;
}
// 取前一個節(jié)點
public E previous() {
    checkForComodification();
    if (!hasPrevious())
        throw new NoSuchElementException();
    // next 為空場景:1:說明是第一次迭代,取尾節(jié)點(last);2:上一次操作把尾節(jié)點刪除掉了
    // next 不為空場景:說明已經(jīng)發(fā)生過迭代了,直接取前一個節(jié)點即可(next.prev)    lastReturned = next = (next == null) ? last : next.prev;
    // 索引位置變化
    nextIndex--;
    return lastReturned.item;
}

刪除:

public void remove() {
    checkForComodification();
    // lastReturned 是本次迭代需要刪除的值,分以下空和非空兩種情況:
    // lastReturned 為空,說明調(diào)用者沒有主動執(zhí)行過 next() 或者 previos(),直接報錯
    // lastReturned 不為空,是在上次執(zhí)行 next() 或者 previos()方法時賦的值
    if (lastReturned == null)
        throw new IllegalStateException();
    Node<E> lastNext = lastReturned.next;
    //刪除當(dāng)前節(jié)點
    unlink(lastReturned);
    // next == lastReturned 的場景分析:從尾到頭遞歸順序,并且是第一次迭代,并且要刪除最后一個元素的情況下
    // 這種情況下,previous() 方法里面設(shè)置了 lastReturned = next = last,所以 next 和 lastReturned會相等
    if (next == lastReturned)
        // 這時候 lastReturned 是尾節(jié)點,lastNext 是 null,所以 next 也是 null,這樣在 previous() 執(zhí)行時,發(fā)現(xiàn) next 是 null,就會把尾節(jié)點賦值給 next
        next = lastNext;
    else
        nextIndex--;
    lastReturned = null;
    expectedModCount++;
}

上述就是小編為大家分享的java中LinkedList有什么用了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

網(wǎng)站標(biāo)題:java中LinkedList有什么用
瀏覽地址:http://bm7419.com/article30/pphgso.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站域名注冊、關(guān)鍵詞優(yōu)化、企業(yè)網(wǎng)站制作品牌網(wǎng)站設(shè)計、網(wǎng)頁設(shè)計公司

廣告

聲明:本網(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)

成都網(wǎng)站建設(shè)公司