java ArrayList.remove()的三种错误用法以及六种正确用法详解

 更新时间:2020年01月03日 15:19:40   作者:逆水_行舟  
这篇文章主要介绍了java ArrayList.remove()的三种错误用法以及六种正确用法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

java集合中,list列表应该是我们最常使用的,它有两种常见的实现类:ArrayList和LinkedList。ArrayList底层是数组,查找比较方便;LinkedList底层是链表,更适合做新增和删除。但实际开发中,我们也会遇到使用ArrayList需要删除列表元素的时候。虽然ArrayList类已经提供了remove方法,不过其中有潜在的坑,下面将介绍remove方法的三种错误用法以及六种正确用法。

1、错误用法

1.1、for循环中使用remove(int index),列表从前往后遍历

首先看一下ArrayList.remove(int index)的源码,读代码前先看方法注释:移除列表指定位置的一个元素,将该元素后面的元素们往左移动一位。返回被移除的元素。

源代码也比较好理解,ArrayList底层是数组,size是数组长度大小,index是数组索引坐标,modCount是被修改次数的计数器,oldValue就是被移除索引的元素对象,numMoved是需要移动的元素数量,如果numMoved大于0,则执行一个数组拷贝(实质是被移除元素后面的元素都向前移动一位)。然后数组长度size减少1,列表最后一位元素置为空。最后将被移除的元素对象返回。

  /**
   * 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循环中调用了多次ArrayList.remove(),那代码执行结果是不准确的,因为每次每次调用remove函数,ArrayList列表都会改变数组长度,被移除元素后面的元素位置都会发生变化。比如下面这个例子,本来是想把列表中奇数位置的元素都移除,但最终得到的结果是[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)大致相同:列表索引坐标从小到大循环遍历,若列表中存在与入参对象相等的元素,则把该元素移除,后面的元素都往左移动一位,返回true,若不存在与入参相等的元素,返回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
  }

如果直接对list调用了该方法,代码结果可能会不准确。例子如下:这段代码本想移除列表中全部值为2的元素,结果并没有成功。

    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是错误用法,我们看一下asList()的源码就能知道了。Arrays.asList()返回的是一个指定数组长度的列表,所以不能做Add、Remove等操作。至于为啥是返回的是固定长度的,看下面源码,asList()函数中调用的new ArrayList<>()并不是我们常用的ArrayList类,而是一个Arrays的内部类,也叫ArrayList,而且这个内部类也是基于数组实现的,但它有一个明显的关键字修饰,那就是final。都用final修饰了,那是肯定不能再对它进行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()这个方法前,我是有点害怕的,毕竟前面两个remove方法都不能直接使用。于是小心翼翼的看了removeIf函数的方法。确认过源码,是我想要的方法!

源码如下:removeIf()的入参是一个过滤条件,用来判断需要移除的元素是否满足条件。方法中设置了一个removeSet,把满足条件的元素索引坐标都放入removeSet,然后统一对removeSet中的索引进行移除。源码相对复杂的是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),结果满足预期。

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

2.2、在for循环之后使用removeAll(Collection<?> c)

这种方法思路是for循环内使用一个集合存放所有满足移除条件的元素,for循环结束后直接使用removeAll方法进行移除。removeAll源码如下,还是比较好理解的:定义了两个数组指针r和w,初始都指向列表第一个元素。循环遍历列表,r指向当前元素,若当前元素没有满足移除条件,将数组[r]元素赋值给数组[w],w指针向后移动一位。这样就完成了整个数组中,没有被移除的元素向前移动。遍历完列表后,将w后面的元素都置空,并减少数组长度。至此完成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转为迭代器Iterator的方式

迭代器就是一个链表,直接使用remove操作不会出现问题。

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

2.4、for循环中使用remove(int index), 列表从后往前遍历

前面1.1也是for循环,为啥从后往前遍历就是正确的呢。因为每次调用remove(int index),index后面的元素会往前移动,如果是从后往前遍历,index后面的元素发生移动,跟index前面的元素无关,我们循环只去和前面的元素做判断,因此就没有影响。

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()之后需要进行add/remove操作,可以使用下面这种方式:

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

2.6、使用while循环

使用while循环,删除了元素,索引便不+1,在没删除元素时索引+1

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Java读取xml文件的五种方式

    Java读取xml文件的五种方式

    在编写与 XML 数据交互的现代软件应用时,有效地读取和解析 XML 文件是至关重要的,本文旨在探讨 Java 中处理 XML 文件的五种主要方法:DOM、SAX、StAX、JAXB 和 JDOM,我们将详细介绍每种方法的工作原理、典型用途以及如何在 Java 程序中实现它们
    2024-05-05
  • Spring Boot 项目创建的详细步骤(图文)

    Spring Boot 项目创建的详细步骤(图文)

    这篇文章主要介绍了Spring Boot 项目创建的详细步骤(图文),这里我们有两种创建Spring Boot项目的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-05-05
  • 详解Java如何获取文件编码格式

    详解Java如何获取文件编码格式

    这篇文章主要介绍了详解Java如何获取文件编码格式,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
    2017-01-01
  • Spring MVC各种参数进行封装的方法实例

    Spring MVC各种参数进行封装的方法实例

    这篇文章主要给大家介绍了关于Spring MVC各种参数进行封装的相关资料,SpringMVC内置多种数据类型转换器,可以根据请求中的参数与后端控制器方法的参数的关系为我们实现简单的数据封装,需要的朋友可以参考下
    2023-06-06
  • 解决java -jar XXX.jar没有主清单属性以及找不到或无法加载主类的问题

    解决java -jar XXX.jar没有主清单属性以及找不到或无法加载主类的问题

    在使用Idea打包SpringBoot项目时,可能会遇到“没有主清单属性”的错误,问题原因是pom文件中缺少配置,未能正确打包成可执行的jar,解决方法包括:1. 修改项目结构并重新生成jar;2. 使用Maven插件在pom文件中添加spring-boot-maven-plugin配置
    2024-09-09
  • Spring Boot 中的 @Field 注解的原理解析

    Spring Boot 中的 @Field 注解的原理解析

    本文详细介绍了 Spring Boot 中的 @Field 注解的原理和使用方法,通过使用 @Field 注解,我们可以将 HTTP 请求中的参数值自动绑定到 Java 对象的属性上,简化了开发过程,提高了开发效率,感兴趣的朋友跟随小编一起看看吧
    2023-07-07
  • Windows 下安装配置 Eclipse详细教程

    Windows 下安装配置 Eclipse详细教程

    Eclipse是一款非常优秀的开源IDE,非常适合Java开发,由于支持插件技术,受到了越来越多的开发者的欢迎。配合众多令人眼花缭乱的插件,完全可以满足从企业级Java应用到手机终端Java游戏的开发。本文将带您手把手步入Eclipse的广阔天地
    2016-09-09
  • IDEA如何修改配置文件的存放位置

    IDEA如何修改配置文件的存放位置

    这篇文章主要介绍了IDEA如何修改配置文件的存放位置,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-12-12
  • springcloud项目快速开始起始模板的实现

    springcloud项目快速开始起始模板的实现

    本文主要介绍了springcloud项目快速开始起始模板思路的实现,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-12-12
  • maven 删除下载失败的包的方法

    maven 删除下载失败的包的方法

    本文介绍了当Maven包报红时,使用删除相关文件的方法来解决该问题,具有一定的参考价值,感兴趣的可以了解一下
    2023-09-09

最新评论