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位置的值就已经被剔除了。

  • 相关阅读:
    Educational Codeforces Round 64 (Rated for Div. 2)题解
    Codeforces Round #555 (Div. 3) F. Maximum Balanced Circle
    莫队算法总结
    cobalt strike使用笔记
    CMD命令行下载文件
    CTF线下赛AWD模式下的生存技巧
    python中multiprocessing模块
    密码重置
    python中的argparse模块
    python中BeautifulSoup模块
  • 原文地址:https://www.cnblogs.com/tao_/p/2192311.html
Copyright © 2011-2022 走看看