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

  • 相关阅读:
    Linux下C语言的调试--转
    linux下c的网络编程---转载
    redis学习资料
    Keepalived配置与使用--转载
    Redis configuration
    keepalived程序包
    Keepalived 使用指南
    myeclipse解决JSP文件script调整背景颜色
    java 面试题汇总(未完成)
    c++ primer plus(文章6版本)中国版 编程练习答案第八章
  • 原文地址:https://www.cnblogs.com/tao_/p/2192311.html
Copyright © 2011-2022 走看看