欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解Java 集合系列(三)—— LinkedList

 更新時間:2019年04月01日 09:49:46   作者:那一葉隨風  
這篇文章主要介紹了Java 集合系列(三)—— LinkedList,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

LinkedList

LinkedList是一種可以在任何位置進行高效地插入和刪除操作的有序序列。
它的最基本存儲結構是一個節(jié)點:每個節(jié)點將存儲對象,以及前后節(jié)點的引用。

結構圖

從上面的結構圖中,我們可以了解到 ListedList 底層是基于雙向鏈表實現(xiàn)的。
圍起來的可以看成 LinkedList 類,它定義了三個 transient 成員變量:first、last、size。這三個變量是整個 LinkedList 類的關鍵點。

  1. 由于是雙向鏈表(每個node都有保存前后節(jié)點的引用),因此我們不管是由 first 還是 last 節(jié)點開始迭代,都可以將整個鏈表的數(shù)據(jù)找出來;
  2. 在查詢、隨機插入以及set等操作都有涉及 size 判斷;
  3. 由于 LinkedList 是雙向鏈表,類中只存儲了首尾兩個節(jié)點,因此查詢第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;        // 將當前最后一個元素寄存在 l
  final Node<E> newNode = new Node<>(l, e, null);  // new 一個新節(jié)點:pre的引用為l;存儲元素為e;next的引用為null
  last = newNode;          // 將新節(jié)點引用覆蓋成員變量 last
  if (l == null)          
   first = newNode;        // 若l為null,說明之前鏈表為空,此時新節(jié)點為首個元素
  else
   l.next = newNode;        // 否則,更新l的next引用
  size++;            // size+1
  modCount++;           // 非查詢操作 modCount 都會 +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é)點前面
 }
 
 
 // 檢查 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 距離哪個鏈表頭近就從哪邊開始開始遍歷
  * 時間復雜度為 O(n/2);
  * 當 index 接近 size 的中間值時,效率最低
  * 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)缺點

優(yōu)點

增刪元素效率高(只需要更新節(jié)點附近的引用即可)

缺點

由于查詢需要進行遍歷,因此效率低

知識腦圖

以上所述是小編給大家介紹的Java 集合系列(三)—— LinkedList詳解整合,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關文章

  • spring啟動錯誤Singleton bean creation not allowed while the singletons of this factory are indestruction

    spring啟動錯誤Singleton bean creation not al

    本文主要介紹了spring啟動錯誤Singleton bean creation not allowed while the singletons of this factory are indestruction,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-07-07
  • SpringBoot之那些注入不了的Spring占位符(${}表達式)問題

    SpringBoot之那些注入不了的Spring占位符(${}表達式)問題

    這篇文章主要介紹了SpringBoot之那些注入不了的Spring占位符(${}表達式)問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • MyBatis通用的10種寫法總結大全

    MyBatis通用的10種寫法總結大全

    這篇文章主要給大家介紹了關于MyBatis通用的10種寫法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2018-11-11
  • RabbitMQ消息有效期與死信的處理過程

    RabbitMQ消息有效期與死信的處理過程

    利用DLX,當消息在一個隊列中變成死信?(dead?message)?之后,它能被重新publish到另一個Exchange,這個Exchange就是DLX,本文重點給大家介紹RabbitMQ消息有效期與死信的相關知識,感興趣的朋友跟隨小編一起看看吧
    2022-03-03
  • spring整合redis以及使用RedisTemplate的方法

    spring整合redis以及使用RedisTemplate的方法

    本篇文章主要介紹了spring整合redis以及使用RedisTemplate的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • mybatisplus駝峰命名映射的問題解決

    mybatisplus駝峰命名映射的問題解決

    本文主要介紹了mybatisplus駝峰命名映射的問題解決,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-07-07
  • SpringBoot中引入MyBatisPlus的常規(guī)操作

    SpringBoot中引入MyBatisPlus的常規(guī)操作

    這篇文章主要介紹了SpringBoot中引入MyBatisPlus的常規(guī)操作,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-11-11
  • 簡述JAVA中堆內(nèi)存與棧內(nèi)存的區(qū)別

    簡述JAVA中堆內(nèi)存與棧內(nèi)存的區(qū)別

    這篇文章主要介紹了JAVA中堆內(nèi)存與棧內(nèi)存的區(qū)別,文中講解非常細致,代碼幫助大家更好的理解和學習,感興趣的朋友可以了解下
    2020-07-07
  • java中對List分段操作的實例

    java中對List分段操作的實例

    這篇文章主要介紹了java中對List分段操作的實例的相關資料,希望通過本文大家能夠掌握list的分段實現(xiàn)方法,需要的朋友可以參考下
    2017-09-09
  • Java發(fā)起http請求的完整步驟記錄

    Java發(fā)起http請求的完整步驟記錄

    這篇文章主要給大家介紹了關于Java發(fā)起http請求的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-02-02

最新評論