【ArrayList源码】remove源码及使用

21 篇文章 0 订阅
14 篇文章 0 订阅

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可以按指定的索引移除值,也可以按指定的值移除。移除某个值后,其后所有的值都会往前移动一位。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值