当前位置 主页 > 服务器问题 > Linux/apache问题 >

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

    栏目:Linux/apache问题 时间:2020-01-04 10:50

    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
      }