zoukankan      html  css  js  c++  java
  • Java中List中remove的实质

    A.如果List的泛型类型为引用类型(Object), 那么,remove只针对List实例所在的栈数据,

    堆数据的移除不由remove完成(是gc完成)。

    B.List移除功能的核心,在Java内部的实现为:

    /* * 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; // Let gc do its work

    }

    核心语句: System.arraycopy(elementData, index+1, elementData, index, numMoved);

    其参数含义分别为:

    src:the source array.

    srcPos:starting position in the source array.

    dest:the destination array.

    destPos:starting position in the destination data.

    length:the number of array elements to be copied.

    该语句可以用下图表示:

    前提条件:index>2。 也就是将数组elementData中从(index+1)开始直到数组的末尾的所有数,

    都依次向前移动一位, 移动后的数组,在最末两位的值是相等的,所以需要清空数组中最后一个数据,

    elementData[--size] = null; // Let gc do its work

    此时,数组(elementData)index位置的值就已经被剔除了。

  • 相关阅读:
    WPF资源字典使用
    忍住你的痛苦
    WPF附加属性
    一致性Hash算法详解
    登录注册
    REST
    资源访问
    参与Bean的生命周期
    sqlserver中查询横表变竖表的sql语句简析
    C#跨线程修改控件——从MSIL和汇编看Invoke, 多线程, 事件与事件委托
  • 原文地址:https://www.cnblogs.com/tao_/p/2192311.html
Copyright © 2011-2022 走看看