【ArrayList源码】remove源码及使用

1 remove源码

在ArrayList源码里,remove有两个方法,一个是按照索引index移除元素,另一个是按照值移除元素。

  • 按索引移除
   /**
     * 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) {
        if (index >= size)
            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));

        modCount++;
        E oldValue = (E) 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;
    }

如果索引值大于ArrayList里元素的个数,则抛出索引越界异常,否则,用oldValue 保存索引处的值。numMoved表示有多少个元素需要移动,或者说元素一共要移动多少步。如果移动步数大于0,则将index之后的元素前移。然后最后一个数组空间设置为null,然后让GC回收它。

  • 按值移除
    /**
     * Removes the first occurrence of the specified element from this list,
     * if it is present.  If the list does not contain the element, it is
     * unchanged.  More formally, removes the element with the lowest index
     * <tt>i</tt> such that
     * <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt>
     * (if such an element exists).  Returns <tt>true</tt> if this list
     * contained the specified element (or equivalently, if this list
     * changed as a result of the call).
     *
     * @param o element to be removed from this list, if present
     * @return <tt>true</tt> if this list contained the specified element
     */
    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;
    }

如果指定的元素为null,则循环查找ArrayList中是否有null元素,如果有则返回true。如果指定元素不为空,则循环,然后将指定元素与ArrayList中的元素进行equals比较,如果相等,则调用fastRemove移除并返回true;其它情况则返回false。fastRemove是ArrayList的私有移除方法,它跳过边界检查并且不会返回值。fastRemove源码如下:

    /*
     * 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
    }

2 remove使用

public static void main(String[] args){
		 ArrayList<String> list = new ArrayList<>();
		 list.add("wo");
		 list.add("ni");
		 list.add("ta");
		 //按索引移除
		 list.remove(0);
		 //按值移除
		 list.remove("ni");
}

3 总结

remove可以按指定的索引移除值,也可以按指定的值移除。移除某个值后,其后所有的值都会往前移动一位。

发布了107 篇原创文章 · 获赞 142 · 访问量 13万+

猜你喜欢

转载自blog.csdn.net/u013293125/article/details/97421431