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

java ArrayList.remove()的三種錯(cuò)誤用法以及六種正確用法詳解

 更新時(shí)間:2020年01月03日 15:19:40   作者:逆水_行舟  
這篇文章主要介紹了java ArrayList.remove()的三種錯(cuò)誤用法以及六種正確用法詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

java集合中,list列表應(yīng)該是我們最常使用的,它有兩種常見的實(shí)現(xiàn)類:ArrayList和LinkedList。ArrayList底層是數(shù)組,查找比較方便;LinkedList底層是鏈表,更適合做新增和刪除。但實(shí)際開發(fā)中,我們也會(huì)遇到使用ArrayList需要?jiǎng)h除列表元素的時(shí)候。雖然ArrayList類已經(jīng)提供了remove方法,不過其中有潛在的坑,下面將介紹remove方法的三種錯(cuò)誤用法以及六種正確用法。

1、錯(cuò)誤用法

1.1、for循環(huán)中使用remove(int index),列表從前往后遍歷

首先看一下ArrayList.remove(int index)的源碼,讀代碼前先看方法注釋:移除列表指定位置的一個(gè)元素,將該元素后面的元素們往左移動(dòng)一位。返回被移除的元素。

源代碼也比較好理解,ArrayList底層是數(shù)組,size是數(shù)組長度大小,index是數(shù)組索引坐標(biāo),modCount是被修改次數(shù)的計(jì)數(shù)器,oldValue就是被移除索引的元素對(duì)象,numMoved是需要移動(dòng)的元素?cái)?shù)量,如果numMoved大于0,則執(zhí)行一個(gè)數(shù)組拷貝(實(shí)質(zhì)是被移除元素后面的元素都向前移動(dòng)一位)。然后數(shù)組長度size減少1,列表最后一位元素置為空。最后將被移除的元素對(duì)象返回。

  /**
   * Removes the element at the specified position in this list.
   * Shifts any subsequent elements to the left (subtracts one from their
   * indices).
   *
   * @param index the index of the element to be removed
   * @return the element that was removed from the list
   * @throws IndexOutOfBoundsException {@inheritDoc}
   */
  public E remove(int index) {
    rangeCheck(index);
 
    modCount++;
    E oldValue = elementData(index);
 
    int numMoved = size - index - 1;
    if (numMoved > 0)
      System.arraycopy(elementData, index+1, elementData, index,
               numMoved);
    elementData[--size] = null; // clear to let GC do its work
 
    return oldValue;
  }

如果在for循環(huán)中調(diào)用了多次ArrayList.remove(),那代碼執(zhí)行結(jié)果是不準(zhǔn)確的,因?yàn)槊看蚊看握{(diào)用remove函數(shù),ArrayList列表都會(huì)改變數(shù)組長度,被移除元素后面的元素位置都會(huì)發(fā)生變化。比如下面這個(gè)例子,本來是想把列表中奇數(shù)位置的元素都移除,但最終得到的結(jié)果是[2,3,5]。

    List<Long> list = new ArrayList<>(Arrays.asList(1L, 2L, 3L, 4L, 5L));
    for (int i = 0; i < list.size(); i++) {
      if (i % 2 == 0) {
        list.remove(i);
      }
    }
    //最終得到[2,3,5]

1.2、直接使用list.remove(Object o)

ArrayList.remove(Object o)源碼的邏輯和ArrayList.remove(int index)大致相同:列表索引坐標(biāo)從小到大循環(huán)遍歷,若列表中存在與入?yún)?duì)象相等的元素,則把該元素移除,后面的元素都往左移動(dòng)一位,返回true,若不存在與入?yún)⑾嗟鹊脑?,返回false。

  public boolean remove(Object o) {
    if (o == null) {
      for (int index = 0; index < size; index++)
        if (elementData[index] == null) {
          fastRemove(index);
          return true;
        }
    } else {
      for (int index = 0; index < size; index++)
        if (o.equals(elementData[index])) {
          fastRemove(index);
          return true;
        }
    }
    return false;
  }
 
  /*
   * Private remove method that skips bounds checking and does not
   * return the value removed.
   */
  private void fastRemove(int index) {
    modCount++;
    int numMoved = size - index - 1;
    if (numMoved > 0)
      System.arraycopy(elementData, index+1, elementData, index,
               numMoved);
    elementData[--size] = null; // clear to let GC do its work
  }

如果直接對(duì)list調(diào)用了該方法,代碼結(jié)果可能會(huì)不準(zhǔn)確。例子如下:這段代碼本想移除列表中全部值為2的元素,結(jié)果并沒有成功。

    List<Long> list = new ArrayList<>(Arrays.asList(1L, 2L, 2L, 4L, 5L));
    list.remove(2L);
    //最終得到[1,2,4,5]

1.3、Arrays.asList()之后使用remove()

為啥使用了Arrays.asList()之后使用remove是錯(cuò)誤用法,我們看一下asList()的源碼就能知道了。Arrays.asList()返回的是一個(gè)指定數(shù)組長度的列表,所以不能做Add、Remove等操作。至于為啥是返回的是固定長度的,看下面源碼,asList()函數(shù)中調(diào)用的new ArrayList<>()并不是我們常用的ArrayList類,而是一個(gè)Arrays的內(nèi)部類,也叫ArrayList,而且這個(gè)內(nèi)部類也是基于數(shù)組實(shí)現(xiàn)的,但它有一個(gè)明顯的關(guān)鍵字修飾,那就是final。都用final修飾了,那是肯定不能再對(duì)它進(jìn)行add/remove操作的。如果非要在Arrays.asList之后使用remove,正確用法參見2.5。

  public static <T> List<T> asList(T... a) {
    return new ArrayList<>(a);
  }
 
  private static class ArrayList<E> extends AbstractList<E>
    implements RandomAccess, java.io.Serializable
   {
    private static final long serialVersionUID = -2764017481108945198L;
    private final E[] a;
 
    ArrayList(E[] array) {
      a = Objects.requireNonNull(array);
    }
  }

2、正確用法

2.1、直接使用removeIf()

使用removeIf()這個(gè)方法前,我是有點(diǎn)害怕的,畢竟前面兩個(gè)remove方法都不能直接使用。于是小心翼翼的看了removeIf函數(shù)的方法。確認(rèn)過源碼,是我想要的方法!

源碼如下:removeIf()的入?yún)⑹且粋€(gè)過濾條件,用來判斷需要移除的元素是否滿足條件。方法中設(shè)置了一個(gè)removeSet,把滿足條件的元素索引坐標(biāo)都放入removeSet,然后統(tǒng)一對(duì)removeSet中的索引進(jìn)行移除。源碼相對(duì)復(fù)雜的是BitSet模型,源碼這里不再貼了。

public boolean removeIf(Predicate<? super E> filter) {
    Objects.requireNonNull(filter);
    // figure out which elements are to be removed
    // any exception thrown from the filter predicate at this stage
    // will leave the collection unmodified
    int removeCount = 0;
    final BitSet removeSet = new BitSet(size);
    final int expectedModCount = modCount;
    final int size = this.size;
    for (int i=0; modCount == expectedModCount && i < size; i++) {
      @SuppressWarnings("unchecked")
      final E element = (E) elementData[i];
      if (filter.test(element)) {
        removeSet.set(i);
        removeCount++;
      }
    }
    if (modCount != expectedModCount) {
      throw new ConcurrentModificationException();
    }
 
    // shift surviving elements left over the spaces left by removed elements
    final boolean anyToRemove = removeCount > 0;
    if (anyToRemove) {
      final int newSize = size - removeCount;
      for (int i=0, j=0; (i < size) && (j < newSize); i++, j++) {
        i = removeSet.nextClearBit(i);
        elementData[j] = elementData[i];
      }
      for (int k=newSize; k < size; k++) {
        elementData[k] = null; // Let gc do its work
      }
      this.size = newSize;
      if (modCount != expectedModCount) {
        throw new ConcurrentModificationException();
      }
      modCount++;
    }
 
    return anyToRemove;
  }

removeIf()的使用方法如下所示(jdk8),結(jié)果滿足預(yù)期。

  List<Long> list = new ArrayList<>(Arrays.asList(1L, 2L, 2L, 4L, 5L));
  list.removeIf(val -> val == 2L);
  //結(jié)果得到[1L,4L,5L]

2.2、在for循環(huán)之后使用removeAll(Collection<?> c)

這種方法思路是for循環(huán)內(nèi)使用一個(gè)集合存放所有滿足移除條件的元素,for循環(huán)結(jié)束后直接使用removeAll方法進(jìn)行移除。removeAll源碼如下,還是比較好理解的:定義了兩個(gè)數(shù)組指針r和w,初始都指向列表第一個(gè)元素。循環(huán)遍歷列表,r指向當(dāng)前元素,若當(dāng)前元素沒有滿足移除條件,將數(shù)組[r]元素賦值給數(shù)組[w],w指針向后移動(dòng)一位。這樣就完成了整個(gè)數(shù)組中,沒有被移除的元素向前移動(dòng)。遍歷完列表后,將w后面的元素都置空,并減少數(shù)組長度。至此完成removeAll移除操作。

public boolean removeAll(Collection<?> c) {
    Objects.requireNonNull(c);
    return batchRemove(c, false);
  }
 
private boolean batchRemove(Collection<?> c, boolean complement) {
    final Object[] elementData = this.elementData;
    int r = 0, w = 0;
    boolean modified = false;
    try {
      for (; r < size; r++)
        if (c.contains(elementData[r]) == complement)
          elementData[w++] = elementData[r];
    } finally {
      // Preserve behavioral compatibility with AbstractCollection,
      // even if c.contains() throws.
      if (r != size) {
        System.arraycopy(elementData, r,
                 elementData, w,
                 size - r);
        w += size - r;
      }
      if (w != size) {
        // clear to let GC do its work
        for (int i = w; i < size; i++)
          elementData[i] = null;
        modCount += size - w;
        size = w;
        modified = true;
      }
    }
    return modified;
  }

正確使用方式如下:

List<Long> removeList = new ArrayList<>();
    for (int i = 0; i < list.size(); i++) {
      if (i % 2 == 0) {
        removeList.add(list.get(i));
      }
    }
    list.removeAll(removeList);

2.3、list轉(zhuǎn)為迭代器Iterator的方式

迭代器就是一個(gè)鏈表,直接使用remove操作不會(huì)出現(xiàn)問題。

Iterator<Integer> it = list.iterator();
while (it.hasNext()) {
 if (it.next() % 2 == 0)
 it.remove();
}

2.4、for循環(huán)中使用remove(int index), 列表從后往前遍歷

前面1.1也是for循環(huán),為啥從后往前遍歷就是正確的呢。因?yàn)槊看握{(diào)用remove(int index),index后面的元素會(huì)往前移動(dòng),如果是從后往前遍歷,index后面的元素發(fā)生移動(dòng),跟index前面的元素?zé)o關(guān),我們循環(huán)只去和前面的元素做判斷,因此就沒有影響。

for (int i = list.size() - 1; i >= 0; i--) {
      if (list.get(i).longValue() == 2) {
        list.remove(i);
      }
    }

2.5、Arrays.asList()之后使用remove()

Arrays.asList()之后需要進(jìn)行add/remove操作,可以使用下面這種方式:

String[] arr = new String[3];
List list = new ArrayList(Arrays.asList(arr));

2.6、使用while循環(huán)

使用while循環(huán),刪除了元素,索引便不+1,在沒刪除元素時(shí)索引+1

int i=0;
while (i<list.size()) {
 if (i % 2 == 0) {
 list.remove(i);
 }else {
 i++;
 }
}

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 基于restTemplate遇到的編碼問題及解決

    基于restTemplate遇到的編碼問題及解決

    這篇文章主要介紹了restTemplate遇到的編碼問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • java中DecimalFormat四舍五入用法詳解

    java中DecimalFormat四舍五入用法詳解

    這篇文章主要為大家詳細(xì)介紹了java中DecimalFormat四舍五入的用法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-03-03
  • SpringBoot2整合Drools規(guī)則引擎及案例詳解

    SpringBoot2整合Drools規(guī)則引擎及案例詳解

    這篇文章主要介紹了SpringBoot2整合Drools規(guī)則引擎及案例詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-10-10
  • 一文搞懂Java?ScheduledExecutorService的使用

    一文搞懂Java?ScheduledExecutorService的使用

    JUC包(java.util.concurrent)中提供了對(duì)定時(shí)任務(wù)的支持,即ScheduledExecutorService接口。本文主要對(duì)ScheduledExecutorService的使用進(jìn)行簡單的介紹,需要的可以參考一下
    2022-11-11
  • 解決rocketmq-spring-boot-starter導(dǎo)致的多消費(fèi)者實(shí)例重復(fù)消費(fèi)問題

    解決rocketmq-spring-boot-starter導(dǎo)致的多消費(fèi)者實(shí)例重復(fù)消費(fèi)問題

    這篇文章主要介紹了解決rocketmq-spring-boot-starter導(dǎo)致的多消費(fèi)者實(shí)例重復(fù)消費(fèi)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-06-06
  • springboot中請(qǐng)求地址轉(zhuǎn)發(fā)的兩種方案

    springboot中請(qǐng)求地址轉(zhuǎn)發(fā)的兩種方案

    在開發(fā)過程中,我們經(jīng)常需要將請(qǐng)求從一個(gè)服務(wù)轉(zhuǎn)發(fā)到另一個(gè)服務(wù),以實(shí)現(xiàn)不同服務(wù)之間的協(xié)作,本文主要介紹了springboot中請(qǐng)求地址轉(zhuǎn)發(fā)的兩種方案,感興趣的可以了解一下
    2023-11-11
  • Spring Boot集成Swagger接口分類與各元素排序問題

    Spring Boot集成Swagger接口分類與各元素排序問題

    這篇文章主要介紹了Spring Boot集成Swagger接口分類與各元素排序問題,首先我們需要對(duì)Swagger中的接口也就是以Controller 層作為第一級(jí)梯度進(jìn)行組織的,Controller在我們實(shí)際開發(fā)中,與其他具體接口之間是存在一對(duì)多的關(guān)系,本文給大家介紹的非常詳細(xì),需要的朋友參考下吧
    2023-10-10
  • IDEA熱部署配置詳細(xì)教程

    IDEA熱部署配置詳細(xì)教程

    這篇文章主要介紹了IDEA熱部署配置詳細(xì)教程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-10-10
  • mybatis-plus  mapper中foreach循環(huán)操作代碼詳解(新增或修改)

    mybatis-plus mapper中foreach循環(huán)操作代碼詳解(新增或修改)

    這篇文章主要介紹了mybatis-plus mapper中foreach循環(huán)操作代碼詳解(新增或修改),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-11-11
  • spring data 連接mongodb的兩種方式

    spring data 連接mongodb的兩種方式

    這篇文章主要介紹了spring data mongodb連接方式詳解,本文給大家分享兩種連接方式,通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2021-08-08

最新評(píng)論