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

  • 相关阅读:
    vue 集成 vis-network 实现网络拓扑图
    三维空间旋转和Three.JS中的实现
    es6常用新属性(详细整理版)
    css的top和left属性不起作用
    网易云音乐歌单生成外链播放器
    Webstorm常用快捷键备忘
    CPU
    中标麒麟neokylin信息查看
    split分割(拆分)文件
    centos7 安装wps
  • 原文地址:https://www.cnblogs.com/tao_/p/2192311.html
Copyright © 2011-2022 走看看