java中的System.arraycopy

  /**
     * @param      src      the source array.源数组
     * @param      srcPos   starting position in the source array.源数组复制的起始位置,会从这里开始复制源数组的元素
     * @param      dest     the destination array.目标数组
     * @param      destPos  starting position in the destination data.目标数组的起始位置。复制的元素会从这个位置开始插入,原本的元素会被覆盖
     * @param      length   the number of array elements to be copied.从源数组中复制几个元素*/
    public static native void arraycopy(Object src,  int  srcPos, Object dest, int destPos, int length);

是一个本地方法,可以用来快速的复制数组

在ArrayList的删除中使用了该方法:

   /**
     * 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)    
          // 假如不是删除最后一个,则将要删除的元素开始,把之后的元素通过copy的方法往前移一位,将需要删除的元素覆盖掉,然后再将最后一个元素置为null等待gc,达到删除目的
            System.arraycopy(elementData, index+1, elementData, index,
                             numMoved);
      elementData[--size] = null; // clear to let GC do its work return oldValue; }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值