成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

LinkedList源碼分析

geekidentity / 548人閱讀

一、屬性及獲取屬性:

1、size

transient int size = 0;

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

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

獲取

public int size() {
    return size;
}
二、構(gòu)造函數(shù)
//Constructs an empty list
public LinkedList() {
}

public LinkedList(Collection c) {
    this();
    addAll(c);
}
三、類
private static class Node {
    E item;
    Node next;
    Node prev;

    Node(Node prev, E element, Node next) {
        this.item = element;
        this.next = next;
        this.prev = prev;
    }
}
四、方法

1、Node

Node node(int index) {
    // assert isElementIndex(index);

    if (index < (size >> 1)) {
        Node x = first;
        for (int i = 0; i < index; i++)
            x = x.next;
        return x;
    } else {
        Node x = last;
        for (int i = size - 1; i > index; i--)
            x = x.prev;
        return x;
    }
}

*、linkFirst

/**
 * Links e as first element.
 */
private void linkFirst(E e) {
    final Node f = first;
    final Node newNode = new Node<>(null, e, f);
    first = newNode;
    if (f == null)
        last = newNode;
    else
        f.prev = newNode;
    size++;
    modCount++;
}

void linkLast(E e) {
    final Node l = last;
    final Node newNode = new Node<>(l, e, null);
    last = newNode;
    if (l == null)
        first = newNode;
    else
        l.next = newNode;
    size++;
    modCount++;
}

*、add、addFirst

public void addFirst(E e) {
    linkFirst(e);
}
public boolean add(E e) {
    linkLast(e);
    return true;
}

linkLast

2、set

public E set(int index, E element) {
    checkElementIndex(index);
    Node x = node(index);
    E oldVal = x.item;
    x.item = element;
    return oldVal;
}

3、get

public E get(int index) {
    checkElementIndex(index);
    return node(index).item;
}

*、clear

public void clear() {
    // Clearing all of the links between nodes is "unnecessary", but:
    // - helps a generational GC if the discarded nodes inhabit
    //   more than one generation
    // - is sure to free memory even if there is a reachable Iterator
    for (Node x = first; x != null; ) {
        Node next = x.next;
        x.item = null;
        x.next = null;
        x.prev = null;
        x = next;
    }
    first = last = null;
    size = 0;
    modCount++;
}

*、Push Pop

public void push(E e) {
    addFirst(e);
}
public E pop() {
    return removeFirst();
}

*、node(int index)

Node node(int index) {
    // assert isElementIndex(index);

    if (index < (size >> 1)) {
        Node x = first;
        for (int i = 0; i < index; i++)
            x = x.next;
        return x;
    } else {
        Node x = last;
        for (int i = size - 1; i > index; i--)
            x = x.prev;
        return x;
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/73424.html

相關(guān)文章

  • LinkedList源碼分析:JDK源碼分析系列

    摘要:介紹是線程不安全的,允許元素為的雙向鏈表。構(gòu)造方法共有兩個構(gòu)造方法,一個是創(chuàng)建一個空的構(gòu)造函數(shù),一個是將已有的添加到中。是將元素插入到的頭部。下一篇文章繼續(xù)分析上次分析了的結(jié)構(gòu)和添加方法這次開始分析下面的。注意源碼版本為直接進(jìn)入正題。 如果本文中有不正確的地方請指出由于沒有留言可以在公眾號添加我的好友共同討論。 1.介紹 LinkedList 是線程不安全的,允許元素為null的雙向鏈...

    blair 評論0 收藏0
  • LinkedList源碼和并發(fā)問題分析

    摘要:在次操作中其實即尾節(jié)點是共享資源,當(dāng)多個線程同時執(zhí)行此方法的時候,其實會出現(xiàn)線程安全問題。同樣會出現(xiàn)并發(fā)安全問題,下面對此問題進(jìn)行分析。 1.LinkedList源碼分析 LinkedList的是基于鏈表實現(xiàn)的java集合類,通過index插入到指定位置的時候使用LinkedList效率要比ArrayList高,以下源碼分析是基于JDK1.8. 1.1 類的繼承結(jié)構(gòu) LinkedLis...

    xietao3 評論0 收藏0
  • 集合框架源碼學(xué)習(xí)之LinkedList

    摘要:它們會在鏈表為空時,拋出獲取尾節(jié)點數(shù)據(jù)方法兩者區(qū)別方法在鏈表為空時,會拋出,而則不會,只是會返回。 目錄: 0-1. 簡介 0-2. 內(nèi)部結(jié)構(gòu)分析 0-3. LinkedList源碼分析   0-3-1. 構(gòu)造方法   0-3-2. 添加add方法     0-3-3. 根據(jù)位置取數(shù)據(jù)的方法   0-3-4. 根據(jù)對象得到索引的方法   0-3-5. 檢查鏈表是否包含某對象的方法  ...

    kumfo 評論0 收藏0
  • LinkedList源碼分析

    摘要:表明該類是可以序列化的。與對比并沒有實現(xiàn),而實現(xiàn)表明其支持快速通常是固定時間隨機(jī)訪問。此接口的主要目的是允許一般的算法更改其行為,從而在將其應(yīng)用到隨機(jī)或連續(xù)訪問列表時能提供良好的性能。這是隨機(jī)訪問效率低的原因之一。指定節(jié)點不能為。 總覽 showImg(https://segmentfault.com/img/bVbsIzr?w=1007&h=600); 定義 public class...

    tommego 評論0 收藏0
  • LinkedList中查詢(contains)和刪除(remove)源碼分析

    摘要:一源碼分析本文分析雙向鏈表的查詢操作源碼實現(xiàn)。中源程序中,的查詢操作,通過函數(shù)實現(xiàn)。源程序中使用循環(huán)進(jìn)行遍歷。表示鏈表元素索引,初值為。針對空元素的情況,用循環(huán)遍歷,查找元素為的節(jié)點,并返回索引。 一、contains源碼分析 本文分析雙向鏈表LinkedList的查詢操作源碼實現(xiàn)。jdk中源程序中,LinkedList的查詢操作,通過contains(Object o)函數(shù)實現(xiàn)。具體...

    timger 評論0 收藏0
  • LinkedList源碼分析

    摘要:源碼分析是一個雙向鏈表的數(shù)據(jù)結(jié)構(gòu)實現(xiàn)。對于支持隨機(jī)訪問數(shù)據(jù)的比如數(shù)組,應(yīng)該優(yōu)先使用。一個有序的集合支持在頭和尾進(jìn)行插入和刪除元素。的大多實現(xiàn)元素數(shù)量是沒有大小限制的。構(gòu)造方法第一個是一個空的構(gòu)造器,第二個構(gòu)造器調(diào)用了方法。 LinkedList源碼分析 LinkedList是一個雙向鏈表的數(shù)據(jù)結(jié)構(gòu)實現(xiàn)。 類的實現(xiàn)接口及繼承父類 public class LinkedList exten...

    andycall 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<