国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

LinkedList源碼解析

roundstones / 2488人閱讀

摘要:我們來看相關(guān)源碼我們看到封裝的和操作其實(shí)就是對(duì)頭結(jié)點(diǎn)的操作。迭代器通過指針,能指向下一個(gè)節(jié)點(diǎn),無需做額外的遍歷,速度非常快。不同的遍歷性能差距極大,推薦使用迭代器進(jìn)行遍歷。

LinkedList類介紹

上一篇文章我們介紹了JDK中ArrayList的實(shí)現(xiàn),ArrayList底層結(jié)構(gòu)是一個(gè)Object[]數(shù)組,通過拷貝,復(fù)制等一系列封裝的操作,將數(shù)組封裝為一個(gè)幾乎是無限的容器。今天我們來介紹JDK中List接口的另外一種實(shí)現(xiàn),基于鏈表結(jié)構(gòu)的LinkedList。ArrayList由于基于數(shù)組,所以在隨機(jī)訪問方面優(yōu)勢(shì)比較明顯,在刪除、插入方面性能會(huì)相對(duì)偏弱些(當(dāng)然與刪除、插入的位置有很大關(guān)系)。那么LinkedList有哪些優(yōu)勢(shì)呢?它在刪除、插入方面的操作很簡單(只是調(diào)整相關(guān)指針而已)。但是隨機(jī)訪問方面要遜色寫。下面我們還是從源碼上來看下這種鏈表結(jié)構(gòu)的List。

LinkedList類主要字段
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;

我們看到字段非常少,size表示當(dāng)前節(jié)點(diǎn)數(shù)量,first指向鏈表的起始元素、last指向鏈表的最后一個(gè)元素。

Node結(jié)構(gòu)

從上面主要字段看出,LinkedList鏈表的Item就是一個(gè)Node結(jié)構(gòu),那么Node結(jié)構(gòu)是怎樣的呢?源碼如下:

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;
    }
}

我們看到Node結(jié)構(gòu)包含一個(gè)前驅(qū)prev指針,item(value)、后繼next指針三個(gè)部分。結(jié)合上面的描述,我們知道了LinkedList的主要結(jié)構(gòu)。如圖:

第一個(gè)節(jié)點(diǎn)的prev指向NULL,最后一個(gè)節(jié)點(diǎn)的next指向NULL。其余節(jié)點(diǎn)通過prev與next串聯(lián)起來,LinkedList提供了從任意節(jié)點(diǎn)都能進(jìn)行向前或先后遍歷的能力。

LinkedList相關(guān)方法解析 構(gòu)造函數(shù)

/**
 * Constructs an empty list.
 */
public LinkedList() {
}

/**
 * Constructs a list containing the elements of the specified
 * collection, in the order they are returned by the collection"s"
 * iterator.
 *
 * @param  c the collection whose elements are to be placed into this list
 * @throws NullPointerException if the specified collection is null
 */
public LinkedList(Collection<");

由于LinkedList是通過prev與next指針鏈接起來的,有元素添加時(shí)只需要一個(gè)個(gè)設(shè)置指針將其鏈接起來即可,所以構(gòu)造函數(shù)相對(duì)較簡潔。我們重點(diǎn)來看下第二個(gè)構(gòu)造函數(shù)中的addAll方法。

addAll方法
/**
 * Appends all of the elements in the specified collection to the end of
 * this list, in the order that they are returned by the specified
 * collection"s iterator.  The behavior of this operation is undefined if
 * the specified collection is modified while the operation is in
 * progress.  (Note that this will occur if the specified collection is
 * this list, and it"s nonempty.)
 *
 * @param c collection containing elements to be added to this list
 * @return {@code true} if this list changed as a result of the call
 * @throws NullPointerException if the specified collection is null
 */
public boolean addAll(Collection<");return addAll(size, c);
}

/**
 * Inserts all of the elements in the specified collection into this
 * list, starting at the specified position.  Shifts the element
 * currently at that position (if any) and any subsequent elements to
 * the right (increases their indices).  The new elements will appear
 * in the list in the order that they are returned by the
 * specified collection"s iterator."
 *
 * @param index index at which to insert the first element
 *              from the specified collection
 * @param c collection containing elements to be added to this list
 * @return {@code true} if this list changed as a result of the call
 * @throws IndexOutOfBoundsException {@inheritDoc}
 * @throws NullPointerException if the specified collection is null
 */
public boolean addAll(int index, Collection<");if (numNew == 0)
        return false;

    Node pred, succ;
    if (index == size) {
        succ = null;
        pred = last;
    } else {
        succ = node(index);
        pred = succ.prev;
    }

    for (Object o : a) {
        @SuppressWarnings("unchecked") E e = (E) o;
        Node newNode = new Node<>(pred, e, null);
        if (pred == null)
            first = newNode;
        else
            pred.next = newNode;
        pred = newNode;
    }

    if (succ == null) {
        last = pred;
    } else {
        pred.next = succ;
        succ.prev = pred;
    }

    size += numNew;
    modCount++;
    return true;
}

/**
 * Returns the (non-null) Node at the specified element 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;
    }
}

addAll方法是將Collection集合插入鏈表。下面我們來仔細(xì)分析整個(gè)過程(涉及比較多的指針操作)。

首先代碼檢查index的值的正確性,如果index位置不合理會(huì)直接拋出異常。

然后將待插入集合轉(zhuǎn)化成數(shù)組,判斷集合長度。

根據(jù)index值,分別設(shè)置pred和succ指針。如果插入的位置是當(dāng)前鏈表尾部,那么pred指向最后一個(gè)元素,succ暫時(shí)設(shè)置為NULL即可。如果插入位置在鏈表中間,那么先通過node方法找到當(dāng)前鏈表的index位置的元素,succ指向它。pred指向待插入位置的前一個(gè)節(jié)點(diǎn),succ指向當(dāng)前index位置的節(jié)點(diǎn),新插入的節(jié)點(diǎn)就是在pred和succ節(jié)點(diǎn)之間。

for循環(huán)創(chuàng)建Node節(jié)點(diǎn),先將pred.next指向新創(chuàng)建的節(jié)點(diǎn),然后pred指向后移,指向新創(chuàng)建的Node節(jié)點(diǎn),重復(fù)上述過程,這樣一個(gè)個(gè)節(jié)點(diǎn)就被創(chuàng)建,鏈接起來了。

最后根據(jù)情況不同,將succ指向的那個(gè)節(jié)點(diǎn)作為最后的節(jié)點(diǎn),當(dāng)然如果succ為NULL的話,last指針指向pred。

removeFirst()方法和removeLast()方法

removeFirst方法會(huì)返回當(dāng)前鏈表的頭部節(jié)點(diǎn)值,然后將頭結(jié)點(diǎn)指向下一個(gè)節(jié)點(diǎn),我們通過源碼來分析:

/**
 * Removes and returns the first element from this list.
 *
 * @return the first element from this list
 * @throws NoSuchElementException if this list is empty
 */
public E removeFirst() {
    final Node f = first;
    if (f == null)
        throw new NoSuchElementException();
    return unlinkFirst(f);
}

/**
 * Unlinks non-null first node f.
 */
private E unlinkFirst(Node f) {
    // assert f == first && f != null;
    final E element = f.item;
    final Node next = f.next;
    f.item = null;
    f.next = null; // help GC
    first = next;
    if (next == null)
        last = null;
    else
        next.prev = null;
    size--;
    modCount++;
    return element;
}

我們看到主要邏輯在unlinkFirst方法中,邏輯還是比較清晰的,first指針指向next節(jié)點(diǎn),該節(jié)點(diǎn)作為新的鏈表頭部,只是最后需要處理下邊界值(next==null)的情況。removeLast方法類似,大家可以去分析源碼。

addFirst方法和addLast()方法

addFirst方法是將新節(jié)點(diǎn)插入鏈表,并且將新節(jié)點(diǎn)作為鏈表頭部,下面我們來看源碼:

/**
 * Inserts the specified element at the beginning of this list.
 *
 * @param e the element to add
 */
public void addFirst(E e) {
    linkFirst(e);
}

/**
 * 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++;
}

代碼邏輯比較清晰,newNode節(jié)點(diǎn)在創(chuàng)建時(shí),由于是作為新的頭結(jié)點(diǎn)的,所以prev必須是NULL的,next是指向當(dāng)前頭結(jié)點(diǎn)f。接下來就是設(shè)置first,處理邊界值了。
下面我們來看下addLast方法,源碼如下:

/**
 * Appends the specified element to the end of this list.
 *
 * 

This method is equivalent to {@link #add}. * * @param e the element to add */ public void addLast(E e) { linkLast(e); } /** * Links e as last element. */ 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方法和remove方法

這兩個(gè)方法是我們使用頻率很高的方法,我們來看下其內(nèi)部實(shí)現(xiàn):

/**
 * Appends the specified element to the end of this list.
 *
 * 

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; } /** * Removes the first occurrence of the specified element from this list, * if it is present. If this list does not contain the element, it is * unchanged. More formally, removes the element with the lowest index * {@code i} such that * (o==null ");if such an element exists). Returns {@code true} if this list * contained the specified element (or equivalently, if this list * changed as a result of the call). * * @param o element to be removed from this list, if present * @return {@code true} if this list contained the specified element */ public boolean remove(Object o) { if (o == null) { for (Node x = first; x != null; x = x.next) { if (x.item == null) { unlink(x); return true; } } } else { for (Node x = first; x != null; x = x.next) { if (o.equals(x.item)) { unlink(x); return true; } } } return false; } /** * Unlinks non-null node x. */ E unlink(Node x) { // assert x != null; final E element = x.item; final Node next = x.next; final Node prev = x.prev; if (prev == null) { first = next; } else { prev.next = next; x.prev = null; } if (next == null) { last = prev; } else { next.prev = prev; x.next = null; } x.item = null; size--; modCount++; return element; }

我們看到add方法其實(shí)就是對(duì)linkLast方法的封裝(當(dāng)然,這是末尾添加)。remove方法邏輯會(huì)復(fù)雜些,需要先找到指定節(jié)點(diǎn),然后調(diào)用unlink方法。
unlink方法解析
我們看到unlink方法首先將需要?jiǎng)h除的節(jié)點(diǎn)的prev和next保存起來,因?yàn)楹竺嫘枰獙烧哌B接起來。然后將prev和next分別判斷設(shè)置(包括邊界值的考慮),最后將x節(jié)點(diǎn)的數(shù)據(jù)設(shè)置為NULL。

clear方法

LinkedList鏈表結(jié)構(gòu)的,它的clear方法是如何實(shí)現(xiàn)的呢?我們來看下:

/**
 * Removes all of the elements from this list.
 * The list will be empty after this call returns.
 */
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++;
}

代碼還是比較清晰的,就是從頭結(jié)點(diǎn)開始,將Node節(jié)點(diǎn)一個(gè)個(gè)的設(shè)置為NULL,方便GC回收。

LinkedList與隊(duì)列操作

有數(shù)據(jù)結(jié)構(gòu)基礎(chǔ)的同學(xué)應(yīng)該都知道隊(duì)列的結(jié)構(gòu),這是一種先進(jìn)先出的結(jié)構(gòu)。從JDK1.5開始,LinkedList內(nèi)部集成了隊(duì)列的操作,LinkedList可以當(dāng)做一個(gè)基本的隊(duì)列進(jìn)行使用。下面我們從隊(duì)列的角度來看下LinkedList提供的相關(guān)方法。

peek、poll、element、remove方法
/**
 * Retrieves, but does not remove, the head (first element) of this list.
 *
 * @return the head of this list, or {@code null} if this list is empty
 * @since 1.5
 */
public E peek() {
    final Node f = first;
    return (f == null) ");return the head of this list
 * @throws NoSuchElementException if this list is empty
 * @since 1.5
 */
public E element() {
    return getFirst();
}

/**
 * Retrieves and removes the head (first element) of this list.
 *
 * @return the head of this list, or {@code null} if this list is empty
 * @since 1.5
 */
public E poll() {
    final Node f = first;
    return (f == null) ");return the head of this list
 * @throws NoSuchElementException if this list is empty
 * @since 1.5
 */
public E remove() {
    return removeFirst();
}

從上面的方法,我們知道peek、element方法只返回隊(duì)列頭部數(shù)據(jù),不移除頭部。而poll、remove方法返回隊(duì)列頭部數(shù)據(jù)的同是,還會(huì)移除頭部。

offer方法
/**
 * Adds the specified element as the tail (last element) of this list.
 *
 * @param e the element to add
 * @return {@code true} (as specified by {@link Queue#offer})
 * @since 1.5
 */
public boolean offer(E e) {
    return add(e);
}

從上面的代碼中我們看到,offer方法其實(shí)就是入隊(duì)操作。

LinkedList與雙端隊(duì)列

上面我們介紹了使用LinkedList來作為隊(duì)列的相關(guān)方法,在JDK6中添加相關(guān)方法讓LinkedList支持雙端隊(duì)列。源代碼如下:

// Deque operations
/**
 * Inserts the specified element at the front of this list.
 *
 * @param e the element to insert
 * @return {@code true} (as specified by {@link Deque#offerFirst})
 * @since 1.6
 */
public boolean offerFirst(E e) {
    addFirst(e);
    return true;
}

/**
 * Inserts the specified element at the end of this list.
 *
 * @param e the element to insert
 * @return {@code true} (as specified by {@link Deque#offerLast})
 * @since 1.6
 */
public boolean offerLast(E e) {
    addLast(e);
    return true;
}

/**
 * Retrieves, but does not remove, the first element of this list,
 * or returns {@code null} if this list is empty.
 *
 * @return the first element of this list, or {@code null}
 *         if this list is empty
 * @since 1.6
 */
public E peekFirst() {
    final Node f = first;
    return (f == null) ");if this list is empty.
 *
 * @return the last element of this list, or {@code null}
 *         if this list is empty
 * @since 1.6
 */
public E peekLast() {
    final Node l = last;
    return (l == null) ");if this list is empty.
 *
 * @return the first element of this list, or {@code null} if
 *     this list is empty
 * @since 1.6
 */
public E pollFirst() {
    final Node f = first;
    return (f == null) ");if this list is empty.
 *
 * @return the last element of this list, or {@code null} if
 *     this list is empty
 * @since 1.6
 */
public E pollLast() {
    final Node l = last;
    return (l == null) ");

上面的代碼邏輯比較清楚,就不詳細(xì)介紹了。

LinkedList與棧(Stack)

堆棧大伙肯定很熟悉,是一種先進(jìn)后出的結(jié)構(gòu)。類似于疊盤子,一般我們使用的時(shí)候肯定從最上面拿取。棧也是這樣,最后進(jìn)入的,最先出去。LinkedList在JDK6的時(shí)候也添加了對(duì)棧的支持。我們來看相關(guān)源碼:

/**
 * Pushes an element onto the stack represented by this list.  In other
 * words, inserts the element at the front of this list.
 *
 * 

This method is equivalent to {@link #addFirst}. * * @param e the element to push * @since 1.6 */ public void push(E e) { addFirst(e); } /** * Pops an element from the stack represented by this list. In other * words, removes and returns the first element of this list. * *

This method is equivalent to {@link #removeFirst()}. * * @return the element at the front of this list (which is the top * of the stack represented by this list) * @throws NoSuchElementException if this list is empty * @since 1.6 */ public E pop() { return removeFirst(); }

我們看到LinkedList封裝的push和pop操作其實(shí)就是對(duì)first頭結(jié)點(diǎn)的操作。通過對(duì)頭結(jié)點(diǎn)不短了的push、pop來模擬堆棧先進(jìn)后出的結(jié)構(gòu)。

LinkedList與迭代器
private class ListItr implements ListIterator {
    private Node lastReturned;
    private Node next;
    private int nextIndex;
    private int expectedModCount = modCount;

    ListItr(int index) {
        // assert isPositionIndex(index);
        next = (index == size) ");hasNext() {
        return nextIndex < size;
    }

    public E next() {
        checkForComodification();
        if (!hasNext())
            throw new NoSuchElementException();

        lastReturned = next;
        next = next.next;
        nextIndex++;
        return lastReturned.item;
    }

    public boolean hasPrevious() {
        return nextIndex > 0;
    }

    public E previous() {
        checkForComodification();
        if (!hasPrevious())
            throw new NoSuchElementException();

        lastReturned = next = (next == null) ");return lastReturned.item;
    }

    public int nextIndex() {
        return nextIndex;
    }

    public int previousIndex() {
        return nextIndex - 1;
    }

    public void remove() {
        checkForComodification();
        if (lastReturned == null)
            throw new IllegalStateException();

        Node lastNext = lastReturned.next;
        unlink(lastReturned);
        if (next == lastReturned)
            next = lastNext;
        else
            nextIndex--;
        lastReturned = null;
        expectedModCount++;
    }

    public void set(E e) {
        if (lastReturned == null)
            throw new IllegalStateException();
        checkForComodification();
        lastReturned.item = e;
    }

    public void add(E e) {
        checkForComodification();
        lastReturned = null;
        if (next == null)
            linkLast(e);
        else
            linkBefore(e, next);
        nextIndex++;
        expectedModCount++;
    }

    public void forEachRemaining(Consumer<");while (modCount == expectedModCount && nextIndex < size) {
            action.accept(next.item);
            lastReturned = next;
            next = next.next;
            nextIndex++;
        }
        checkForComodification();
    }

    final void checkForComodification() {
        if (modCount != expectedModCount)
            throw new ConcurrentModificationException();
    }
}

從上面的迭代器的源碼我們可以知道以下幾點(diǎn):

1、LinkedList通過自定義迭代器實(shí)現(xiàn)了往前往后兩個(gè)方向的遍歷。

2、remove方法中next == lastReturned條件的判斷是針對(duì)上一次對(duì)鏈表進(jìn)行了previous操作后進(jìn)行的判斷。因?yàn)樯弦淮蝡revious操作后next指針會(huì)“懸空”。需要將其設(shè)置為next節(jié)點(diǎn)。

LinkedList遍歷相關(guān)問題

對(duì)于集合來說,遍歷是非常常規(guī)的操作。但是對(duì)于LinkedList來說,遍歷的時(shí)候需要選擇合適的方法,因?yàn)椴缓侠淼姆椒▽?duì)于性能有非常大的差別。我們通過例子來看:

List list=new LinkedList<>();
for(int i=0;i<10000;i++) {
	list.add(String.valueOf(i));
}

//遍歷方法一
long time=System.currentTimeMillis();
for(int i=0;i iterator=list.iterator();
while (iterator.hasNext()) {
	iterator.next();
}
iterator.remove();
System.out.println(System.currentTimeMillis()-time);

輸出如下:

size:10000的情況
120
2
size:100000的情況
28949
2

同樣是遍歷方法,為什么性能差別幾十倍,設(shè)置上萬倍呢?研究過源碼的同學(xué)應(yīng)該能發(fā)現(xiàn)其中的奧秘。我們來看get方法的邏輯:

/**
 * Returns the element at the specified position in this list.
 *
 * @param index index of the element to return
 * @return the element at the specified position in this list
 * @throws IndexOutOfBoundsException {@inheritDoc}
 */
public E get(int index) {
    checkElementIndex(index);
    return node(index).item;
}

/**
 * Returns the (non-null) Node at the specified element 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;
    }
}

我們看到,我們get(index)的時(shí)候,都需要從頭,或者從尾部慢慢循環(huán)過來。get(4000)的時(shí)候需要從0-4000進(jìn)行遍歷。get(4001)的時(shí)候還是需要從0-4001進(jìn)行遍歷。做了無數(shù)的無用功。但是迭代器就不一樣了。迭代器通過next指針,能指向下一個(gè)節(jié)點(diǎn),無需做額外的遍歷,速度非常快。

總結(jié)

1、LinkedList在添加及修改時(shí)候效率較高,只需要設(shè)置前后節(jié)點(diǎn)即可(ArrayList還需要拷貝前后數(shù)據(jù))。

2、LinkedList不同的遍歷性能差距極大,推薦使用迭代器進(jìn)行遍歷。LinkedList在隨機(jī)訪問方面性能一般(ArrayList隨機(jī)方法可以使用基地址+偏移量的方式訪問)

LinkedList提供作為隊(duì)列、堆棧的相關(guān)方法。

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

轉(zhuǎn)載請(qǐng)注明本文地址:http://specialneedsforspecialkids.com/yun/7025.html

相關(guān)文章

  • Java集合之LinkedList源碼解析

    摘要:快速失敗在用迭代器遍歷一個(gè)集合對(duì)象時(shí),如果遍歷過程中對(duì)集合對(duì)象的內(nèi)容進(jìn)行了修改增加刪除修改,則會(huì)拋出。原理由于迭代時(shí)是對(duì)原集合的拷貝進(jìn)行遍歷,所以在遍歷過程中對(duì)原集合所作的修改并不能被迭代器檢測到,所以不會(huì)觸發(fā)。 原文地址 LinkedList 在Java.util包下 繼承自AbstractSequentialList 實(shí)現(xiàn) List 接口,能對(duì)它進(jìn)行隊(duì)列操作。 實(shí)現(xiàn) Deque ...

    DC_er 評(píng)論0 收藏0
  • LinkedList 基本示例及源碼解析

    摘要:對(duì)于不可修改的列表來說,程序員需要實(shí)現(xiàn)列表迭代器的和方法介紹這個(gè)接口也是繼承類層次的核心接口,以求最大限度的減少實(shí)現(xiàn)此接口的工作量,由順序訪問數(shù)據(jù)存儲(chǔ)例如鏈接鏈表支持。 一、JavaDoc 簡介 LinkedList雙向鏈表,實(shí)現(xiàn)了List的 雙向隊(duì)列接口,實(shí)現(xiàn)了所有l(wèi)ist可選擇性操作,允許存儲(chǔ)任何元素(包括null值) 所有的操作都可以表現(xiàn)為雙向性的,遍歷的時(shí)候會(huì)從首部到尾部進(jìn)行...

    senntyou 評(píng)論0 收藏0
  • LinkedList源碼解析

    摘要:我們來看相關(guān)源碼我們看到封裝的和操作其實(shí)就是對(duì)頭結(jié)點(diǎn)的操作。迭代器通過指針,能指向下一個(gè)節(jié)點(diǎn),無需做額外的遍歷,速度非常快。不同的遍歷性能差距極大,推薦使用迭代器進(jìn)行遍歷。LinkedList類介紹 上一篇文章我們介紹了JDK中ArrayList的實(shí)現(xiàn),ArrayList底層結(jié)構(gòu)是一個(gè)Object[]數(shù)組,通過拷貝,復(fù)制等一系列封裝的操作,將數(shù)組封裝為一個(gè)幾乎是無限的容器。今天我們來介紹JD...

    番茄西紅柿 評(píng)論0 收藏0
  • LinkedList源碼解析

    摘要:我們來看相關(guān)源碼我們看到封裝的和操作其實(shí)就是對(duì)頭結(jié)點(diǎn)的操作。迭代器通過指針,能指向下一個(gè)節(jié)點(diǎn),無需做額外的遍歷,速度非常快。不同的遍歷性能差距極大,推薦使用迭代器進(jìn)行遍歷。LinkedList類介紹 上一篇文章我們介紹了JDK中ArrayList的實(shí)現(xiàn),ArrayList底層結(jié)構(gòu)是一個(gè)Object[]數(shù)組,通過拷貝,復(fù)制等一系列封裝的操作,將數(shù)組封裝為一個(gè)幾乎是無限的容器。今天我們來介紹JD...

    番茄西紅柿 評(píng)論0 收藏0
  • LinkedList源碼解析(一)

    摘要:基本屬性存儲(chǔ)數(shù)據(jù)量指向第一個(gè)節(jié)點(diǎn)的指針指向最后一個(gè)節(jié)點(diǎn)的指針。 基本屬性 transient int size = 0;//存儲(chǔ)數(shù)據(jù)量 /** * Pointer to first node. */ transient Node first;//指向第一個(gè)節(jié)點(diǎn)的指針 /** * Pointer to last node...

    GT 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<