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

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

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

LinkedList

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

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

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

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

優(yōu)點(diǎn)

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

缺點(diǎn)

由于查詢需要進(jìn)行遍歷,因此效率低

知識(shí)腦圖

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

相關(guān)文章

  • spring啟動(dòng)錯(cuò)誤Singleton bean creation not allowed while the singletons of this factory are indestruction

    spring啟動(dòng)錯(cuò)誤Singleton bean creation not al

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

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

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

    MyBatis通用的10種寫法總結(jié)大全

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

    RabbitMQ消息有效期與死信的處理過(guò)程

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

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

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

    mybatisplus駝峰命名映射的問(wèn)題解決

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

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

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

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

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

    java中對(duì)List分段操作的實(shí)例

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

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

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

最新評(píng)論