亚洲中字慕日产2020,大陆极品少妇内射AAAAAA,无码av大香线蕉伊人久久,久久精品国产亚洲av麻豆网站

資訊專欄INFORMATION COLUMN

LinkedList源碼解析(二)

Ashin / 2049人閱讀

摘要:返回結(jié)合中存儲(chǔ)的節(jié)點(diǎn)數(shù)量向集合末尾添加一個(gè)元素移除一個(gè)元素如果是從第一個(gè)節(jié)點(diǎn)指針指向的節(jié)點(diǎn)開始循環(huán)比較節(jié)點(diǎn)的值,的內(nèi)存地址取消節(jié)點(diǎn)操作成功如果是不是從第一個(gè)節(jié)點(diǎn)指針指向的節(jié)點(diǎn)開始循環(huán)調(diào)用的方法和節(jié)點(diǎn)的值作比較取消節(jié)點(diǎn)操作成功操作失敗向集合末

size()返回結(jié)合中存儲(chǔ)的節(jié)點(diǎn)數(shù)量

public int size() {
        return size;
    }

add(E e)向集合末尾添加一個(gè)元素

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

remove(Object o)移除一個(gè)元素

public boolean remove(Object o) {
        if (o == null) {//如果o是null
            for (Node x = first; x != null; x = x.next) {//從第一個(gè)節(jié)點(diǎn)指針指向的節(jié)點(diǎn)開始循環(huán)
                if (x.item == null) {//比較節(jié)點(diǎn)的值,的內(nèi)存地址
                    unlink(x);//取消節(jié)點(diǎn)
                    return true;//操作成功
                }
            }
        } else {//如果o是不是null
            for (Node x = first; x != null; x = x.next) {//從第一個(gè)節(jié)點(diǎn)指針指向的節(jié)點(diǎn)開始循環(huán)
                if (o.equals(x.item)) {//調(diào)用o的equals方法和節(jié)點(diǎn)的值作比較
                    unlink(x);//取消節(jié)點(diǎn)
                    return true;//操作成功
                }
            }
        }
        return false;//操作失敗
    }

addAll(Collection c)向集合末尾加入集合c

public boolean addAll(Collection c) {
        return addAll(size, c);
    }

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; ) {//從first指針指向的節(jié)點(diǎn)開始循環(huán)
            Node next = x.next;//獲取x的next
            x.item = null;//x的值置空
            x.next = null;//x的next置空
            x.prev = null;//x的prev置空
            x = next;//x賦值為next下一次循環(huán)使用
        }
        first = last = null;//第一個(gè)節(jié)點(diǎn)指針和最后一個(gè)節(jié)點(diǎn)的指針置空
        size = 0;//數(shù)據(jù)長(zhǎng)度0
        modCount++;//操作數(shù)不清空
    }

get(int index)獲取index索引節(jié)點(diǎn)數(shù)據(jù)

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

set(int index, E element)設(shè)置index索引處的節(jié)點(diǎn)位數(shù)為element

public E set(int index, E element) {
        checkElementIndex(index);//index在范圍內(nèi)
        Node x = node(index);//獲取索引處的節(jié)點(diǎn)
        E oldVal = x.item;//獲取節(jié)點(diǎn)舊的值
        x.item = element;//給節(jié)點(diǎn)的值賦值新值
        return oldVal;//返回舊的值
    }

add(int index, E element)根據(jù)索引插入數(shù)據(jù)

 public void add(int index, E element) {
        checkPositionIndex(index);//index在范圍內(nèi)

        if (index == size)/、如果索引位index等于數(shù)據(jù)長(zhǎng)度
            linkLast(element);//尾插入
        else
            linkBefore(element, node(index));//否則插入在index索引對(duì)應(yīng)節(jié)點(diǎn)之前
    }

remove(int index)移除索引index處的數(shù)據(jù)

 public E remove(int index) {
        checkElementIndex(index);//index在范圍內(nèi)
        return unlink(node(index));
    }

isElementIndex(int index)判斷參數(shù)是否是現(xiàn)有元素的索引

  private boolean isElementIndex(int index) {
        return index >= 0 && index < size;
    }

isPositionIndex(int index)判斷參數(shù)是否是現(xiàn)有元素的索引(迭代器或添加操作)

  private boolean isPositionIndex(int index) {
        return index >= 0 && index < size;
    }

構(gòu)造一個(gè)IndexOutOfBoundsException詳細(xì)消息

 private String outOfBoundsMsg(int index) {
        return "Index: "+index+", Size: "+size;
    }

    private void checkElementIndex(int index) {
        if (!isElementIndex(index))
            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
    }

    private void checkPositionIndex(int index) {
        if (!isPositionIndex(index))
            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
    }

lastIndexOf(Object o)返回指定元素最后一次出現(xiàn)的索引

 public int lastIndexOf(Object o) {
        int index = size;//初始下標(biāo)賦值
        if (o == null) {//o為null
            for (Node x = last; x != null; x = x.prev) {//last指針指向的節(jié)點(diǎn)開始向前循環(huán)
                index--;
                if (x.item == null)//節(jié)點(diǎn)的值作內(nèi)存比較
                    return index;//返回下標(biāo)
            }
        } else {//o不為null
            for (Node x = last; x != null; x = x.prev) {//last指針指向的節(jié)點(diǎn)開始向前循環(huán)
                index--;
                if (o.equals(x.item))//調(diào)用o的equals方法和節(jié)點(diǎn)的值比較
                    return index;
            }
        }
        return -1;
    }

peek()索但不刪除此列表的頭部(null返回null)

 public E peek() {
        final Node f = first;
        return (f == null) ? null : f.item;//如果是null的話不返回對(duì)象,返回null
    }

element()檢索但不刪除此列表的頭部(null會(huì)拋出異常)

public E element() {
        return getFirst();
    }

getFirst()返回此列表中的第一個(gè)元素(null會(huì)拋出異常)

public E getFirst() {
        final Node f = first;
        if (f == null)
            throw new NoSuchElementException();
        return f.item;
    }

poll()檢索并刪除此列表的頭部(null返回null)

 public E poll() {
        final Node f = first;
        return (f == null) ? null : unlinkFirst(f);//不為null時(shí)候,刪除并返回第一個(gè)節(jié)點(diǎn)
    }

remove()檢索并刪除此列表的頭部

  public E remove() {
        return removeFirst();
    }

offer(E e)將指定的元素添加為此列表的尾部

public boolean offer(E e) {
        return add(e);
    }

offerFirst(E e)在指定列表第一個(gè)節(jié)點(diǎn)前面插入e

 public boolean offerFirst(E e) {
        addFirst(e);
        return true;
    }

offerLast(E e)在指定列表最后一個(gè)節(jié)點(diǎn)后面插入e

  public boolean offerLast(E e) {
        addLast(e);
        return true;
    }

peekFirst()檢索但不刪除此列表的第一個(gè)節(jié)點(diǎn)(null返回null)

 public E peekFirst() {
        final Node f = first;
        return (f == null) ? null : f.item;
     }

peekFirst()檢索但不刪除此列表的最后一個(gè)節(jié)點(diǎn)(null返回null)

 public E peekLast() {
        final Node l = last;
        return (l == null) ? null : l.item;
    }

pollFirst()檢索并刪除此列表的第一個(gè)節(jié)點(diǎn)(null返回null)

public E pollFirst() {
        final Node f = first;
        return (f == null) ? null : unlinkFirst(f);
    }

pollLast()檢索并刪除此列表的第最后一個(gè)節(jié)點(diǎn)(null返回null)

 public E pollLast() {
        final Node l = last;
        return (l == null) ? null : unlinkLast(l);
    }

push(E e)將元素插入到第一個(gè)節(jié)點(diǎn)簽名

public void push(E e) {
        addFirst(e);
    }

pop()移除第一個(gè)節(jié)點(diǎn)

  public E pop() {
        return removeFirst();
    }

removeFirstOccurrence(Object o)刪除此中第一次出現(xiàn)的指定元素

public boolean removeFirstOccurrence(Object o) {
        return remove(o);
    }

removeLastOccurrence(Object o)刪除此中最后一次出現(xiàn)的指定元素

//和lastIndexOf類似,找到后直接調(diào)用unlink
 public boolean removeLastOccurrence(Object o) {
        if (o == null) {
            for (Node x = last; x != null; x = x.prev) {
                if (x.item == null) {
                    unlink(x);
                    return true;
                }
            }
        } else {
            for (Node x = last; x != null; x = x.prev) {
                if (o.equals(x.item)) {
                    unlink(x);
                    return true;
                }
            }
        }
        return false;
    }

ListIterator listIterator(int index)返回集合迭代器

 public ListIterator listIterator(int index) {
        checkPositionIndex(index);
        return new ListItr(index);
    }
迭代器類ListItr
  private class ListItr implements ListIterator {
        private Node lastReturned;//最后返回的節(jié)點(diǎn)
        private Node next;//下一個(gè)節(jié)點(diǎn)
        private int nextIndex;//下一個(gè)節(jié)點(diǎn)的索引
        private int expectedModCount = modCount;

        ListItr(int index) {
            // assert isPositionIndex(index);
            next = (index == size) ? null : node(index);//構(gòu)造下一個(gè)節(jié)點(diǎn)的索引
            nextIndex = index;
        }

        public boolean hasNext() {
            return nextIndex < size;//判斷是否有下一個(gè)節(jié)點(diǎn)
        }

        public E next() {
            checkForComodification();//線程安全
            if (!hasNext())
                throw new NoSuchElementException();//迭代器到尾部

            lastReturned = next;//迭代器越過next
            next = next.next;//next賦值為next的下一個(gè)節(jié)點(diǎn)
            nextIndex++;//下一個(gè)節(jié)點(diǎn)的索引+1
            return lastReturned.item;//返回迭代器越過節(jié)點(diǎn)的值
        }

        public boolean hasPrevious() {
            return nextIndex > 0;//是否有前一個(gè)節(jié)點(diǎn)
        }

        public E previous() {
            checkForComodification();//線程安全
            if (!hasPrevious())
                throw new NoSuchElementException();//迭代器到達(dá)頭部

            lastReturned = next = (next == null) ? last : next.prev;//如果是空返回last指針指向的節(jié)點(diǎn)(不理解)
            nextIndex--;//下一個(gè)節(jié)點(diǎn)索引自減
            return lastReturned.item;//返回迭代器越過節(jié)點(diǎn)的值
        }

        public int nextIndex() {
            return nextIndex;//返回下一個(gè)索引
        }

        public int previousIndex() {
            return nextIndex - 1;//返回上一個(gè)索引
        }

        public void remove() {
            checkForComodification();
            if (lastReturned == null)//迭代器沒有越過任何元素
                throw new IllegalStateException();

            Node lastNext = lastReturned.next;//獲取迭代器越過節(jié)點(diǎn)的下一個(gè)節(jié)點(diǎn)
            unlink(lastReturned);//移除越過的元素
            if (next == lastReturned)//不理解為什么會(huì)進(jìn)去
                next = lastNext;
            else
                nextIndex--;//下一個(gè)節(jié)點(diǎn)索引自減
            lastReturned = null;
            expectedModCount++;
        }

        public void set(E e) {
            if (lastReturned == null)//迭代器沒有越過任何元素
                throw new IllegalStateException();
            checkForComodification();//線程安全
            lastReturned.item = e;//迭代器越過節(jié)點(diǎn)的值
        }

        public void add(E e) {
            checkForComodification();//線程安全
            lastReturned = null;
            if (next == null)//尾巴插入
                linkLast(e);
            else
                linkBefore(e, next);//next節(jié)點(diǎn)前插入
            nextIndex++;//下一個(gè)節(jié)點(diǎn)的索引加1
            expectedModCount++;
        }

        public void forEachRemaining(Consumer action) {
            Objects.requireNonNull(action);
            while (modCount == expectedModCount && nextIndex < size) {//下一個(gè)節(jié)點(diǎn)的索引小于節(jié)點(diǎn)數(shù)
                action.accept(next.item);//運(yùn)行accept方法
                lastReturned = next;
                next = next.next;
                nextIndex++;
            }
            checkForComodification();//線程安全
        }

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

descendingIterator()適配器通過ListItr.previous提供降序迭代器

 public Iterator descendingIterator() {
        return new DescendingIterator();
    }
降序迭代器DescendingIterato(調(diào)用的就是ListItr,反著調(diào)用)
 private class DescendingIterator implements Iterator {
        private final ListItr itr = new ListItr(size());
        public boolean hasNext() {
            return itr.hasPrevious();
        }
        public E next() {
            return itr.previous();
        }
        public void remove() {
            itr.remove();
        }
    }

superClone()超類復(fù)制

@SuppressWarnings("unchecked")
private LinkedList superClone() {
        try {
            return (LinkedList) super.clone();
        } catch (CloneNotSupportedException e) {
            throw new InternalError(e);
        }
    }

clone()復(fù)制集合對(duì)象

 public Object clone() {
        LinkedList clone = superClone();

        // Put clone into "virgin" state
        clone.first = clone.last = null;//第一個(gè)節(jié)點(diǎn)和最后一個(gè)節(jié)點(diǎn)置空
        clone.size = 0;//數(shù)據(jù)數(shù)置0
        clone.modCount = 0;//操作數(shù)置0

        // Initialize clone with our elements
        for (Node x = first; x != null; x = x.next)//從first節(jié)點(diǎn)開始循環(huán)初始化clone對(duì)象
            clone.add(x.item);

        return clone;
    }

toArray()返回集合元素組成的數(shù)組

  public Object[] toArray() {
        Object[] result = new Object[size];
        int i = 0;
        for (Node x = first; x != null; x = x.next)
            result[i++] = x.item;
        return result;
    }

toArray(T[] a)返回集合元素組成的數(shù)組(傳入數(shù)組的類型)

 @SuppressWarnings("unchecked")
    public  T[] toArray(T[] a) {
        if (a.length < size)
            a = (T[])java.lang.reflect.Array.newInstance(
                                a.getClass().getComponentType(), size);//創(chuàng)建數(shù)組
        int i = 0;
        Object[] result = a;
        for (Node x = first; x != null; x = x.next)
            result[i++] = x.item;

        if (a.length > size)
            a[size] = null;

        return a;
    }

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

轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/74936.html

相關(guān)文章

  • LinkedList 基本示例及源碼解析

    摘要:對(duì)于不可修改的列表來說,程序員需要實(shí)現(xiàn)列表迭代器的和方法介紹這個(gè)接口也是繼承類層次的核心接口,以求最大限度的減少實(shí)現(xiàn)此接口的工作量,由順序訪問數(shù)據(jù)存儲(chǔ)例如鏈接鏈表支持。 一、JavaDoc 簡(jiǎn)介 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
  • List集合就這么簡(jiǎn)單【源碼剖析】

    摘要:線程不安全底層數(shù)據(jù)結(jié)構(gòu)是鏈表。的默認(rèn)初始化容量是,每次擴(kuò)容時(shí)候增加原先容量的一半,也就是變?yōu)樵瓉淼谋秳h除元素時(shí)不會(huì)減少容量,若希望減少容量則調(diào)用它不是線程安全的。 前言 聲明,本文用得是jdk1.8 前一篇已經(jīng)講了Collection的總覽:Collection總覽,介紹了一些基礎(chǔ)知識(shí)。 現(xiàn)在這篇主要講List集合的三個(gè)子類: ArrayList 底層數(shù)據(jù)結(jié)構(gòu)是數(shù)組。線程不安全 ...

    cpupro 評(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...

    roundstones 評(píng)論0 收藏0
  • Java集合之LinkedList源碼解析

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

    DC_er 評(píng)論0 收藏0
  • java源碼

    摘要:集合源碼解析回歸基礎(chǔ),集合源碼解析系列,持續(xù)更新和源碼分析與是兩個(gè)常用的操作字符串的類。這里我們從源碼看下不同狀態(tài)都是怎么處理的。 Java 集合深入理解:ArrayList 回歸基礎(chǔ),Java 集合深入理解系列,持續(xù)更新~ JVM 源碼分析之 System.currentTimeMillis 及 nanoTime 原理詳解 JVM 源碼分析之 System.currentTimeMi...

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

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

0條評(píng)論

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