ArrayList add remove 代码分析
Add
首次add 元素需要对数组进行扩容(初始化Capacity 10, 第二次扩容10>>1 为5, 第三次扩容15>>1 为7), 每次扩容之前长度的1.5倍,当add 的数据较多时扩容较为频繁,这时建议在new ArrayList() 指定初始容量 或者 使用 linkedList
public boolean add(E e) {
ensureCapacityInternal(size + 1); // Increments modCount!!
elementData[size++] = e;
return true;
}
// 确定内存容量
private void ensureCapacityInternal(int minCapacity) { //minCapacity=1 增加的容量
if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) { // DEFAULTCAPACITY_EMPTY_ELEMENTDATA=10 //默认的容量
minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
}
// 明确容量
ensureExplicitCapacity(minCapacity); // minCapacity=10
}
/**
* Increases the capacity to ensure that it can hold at least the
* number of elements specified by the minimum capacity argument.
* 确保数组中的容量能存储的下新增的容量,不足则扩容
* @param minCapacity the desired minimum capacity
*/
private void grow(int minCapacity) { //minCapacity=10
// overflow-conscious code
int oldCapacity = elementData.length; //oldCapacity =0
int newCapacity = oldCapacity + (oldCapacity >> 1); //newCapacity =0
if (newCapacity - minCapacity < 0)
newCapacity = minCapacity; //newCapacity =10
if (newCapacity - MAX_ARRAY_SIZE > 0)
newCapacity = hugeCapacity(minCapacity);
// minCapacity is usually close to size, so this is a win:
elementData = Arrays.copyOf(elementData, newCapacity);// 调用system.arraycopy 对原数组进行扩容
}
remove
除非是当前数组中只剩下一个元素,否则每次remove 都会 System.arraycopy 移动元素。
如果是remove(obj) 需要每次遍历数组找到equals的对象,然后 System.arraycopy 移动元素。
LinkedList 虽然不用移位,但也每次都要变量链表,更改引用对象
public E remove(int index) {
rangeCheck(index); // 校验要删除的索引是否超出范围 modCount++;// 操作count++
E oldValue = elementData(index);// 取出要删除的元素 int numMoved = size - index - 1; // 得出要移动元素个数
if (numMoved > 0)
// 移动元素
System.arraycopy(elementData, index+1, elementData, index,
numMoved);
// 将删除的元素置为null
elementData[--size] = null; // clear to let GC do its work return oldValue;
}
get
E elementData(int index) {
// 直接根据索引获取元素
return (E) elementData[index];
}
ArrayList add remove 代码分析的更多相关文章
- VSM(Virtual Storage Manager) add new osd 代码分析
vsm add new osd 流程 后台获取可用设备 | 选择可用设备及osd相关信息等 | 点击add按钮,把准备部署的osd信息添加到需要部署的item列表中 | 点击submit按钮,添加it ...
- HashSet——add remove contains方法底层代码分析(hashCode equals 方法的重写)
引言:我们都知道HashSet这个类有add remove contains方法,但是我们要深刻理解到底是怎么判断它是否重复加入了,什么时候才移除,什么时候才算是包括????????? add ...
- ArrayList代码分析
集合算是java中最常用的部分了,阅读该部分jdk代码可以让我们更加清楚的了解其实现原理,在使用时也能心中有数,有利于写出高质量的代码. ArrayList 底层数组实现,初始长度10,超过长度后的自 ...
- ArrayList 源码详细分析
1.概述 ArrayList 是一种变长的集合类,基于定长数组实现.ArrayList 允许空值和重复元素,当往 ArrayList 中添加的元素数量大于其底层数组容量时,其会通过扩容机制重新生成一个 ...
- Hive metastore整体代码分析及详解
从上一篇对Hive metastore表结构的简要分析中,我再根据数据设计的实体对象,再进行整个代码结构的总结.那么我们先打开metadata的目录,其目录结构: 可以看到,整个hivemeta的目录 ...
- Android版数据结构与算法(二):基于数组的实现ArrayList源码彻底分析
版权声明:本文出自汪磊的博客,未经作者允许禁止转载. 本片我们分析基础数组的实现--ArrayList,不会分析整个集合的继承体系,这不是本系列文章重点. 源码分析都是基于"安卓版" ...
- ArrayList迭代器源码分析
集合的遍历 Java集合框架中容器有很多种类,如下图中: 对于有索引的List集合可以通过for循环遍历集合: List<String> list = new ArrayList<& ...
- 有关集合的foreach循环里的add/remove
转自:Hollis(微信号:hollischuang) 在阿里巴巴Java开发手册中,有这样一条规定: 但是手册中并没有给出具体原因,本文就来深入分析一下该规定背后的思考. 1 .foreach循环 ...
- Java foreach remove问题分析
原文链接:http://www.cnblogs.com/chrischennx/p/9610853.html 都说ArrayList在用foreach循环的时候,不能add元素,也不能remove元素 ...
随机推荐
- EL的函数与标签
1 什么EL函数库 EL函数库是由第三方对EL的扩展,我们现在学习的EL函数库是由JSTL添加的.下面我们会学习JSTL标签库. EL函数库就是定义一些有返回值的静态方法.然后通过EL语言来调用它们! ...
- Count Numbers with Unique Digits -- LeetCode
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10^n. Exam ...
- Codeforces 920 E Connected Components?
Discription You are given an undirected graph consisting of n vertices and edges. Instead of giving ...
- 【bzoj1001】【最短路】【对偶图】【最大流转最小割】狼抓兔子题解
[BZOJ1001]狼抓兔子 1001: [BeiJing2006]狼抓兔子 Time Limit: 15 Sec Memory Limit: 162 MBSubmit: 18872 Solved ...
- [读书笔记] CSS揭秘-背景与边框
半透明边框 默认情况下,背景会延伸到边框所在的区域下层.可以通过background-clip属性调整该默认行为. border: 10px solid rgba(0,0,0,.2) backgrou ...
- apache 单独生成模块
apache 单独生成模块 一般这种模块都是后期自己去生成的,比如一般在安装apache时都会--enable-so ,允许动态加载模块. 这个模块你可以这样去生成. 1.下载一个与当前使用的apa ...
- VMware Server中虚拟机随宿主机自动启动
在options页面, 开启 Start Up and Shut Down Virtual Machines 这个选项. 保存退出. 打开 VMWare Server Console, 打开需要自动启 ...
- 能上架App的GooglePlay开发者账号获取流程
googleplay 开发者账号申请流程 接到公司号召,要让我们的app走向世界,上架GooglePlay,都说天朝的Android 程序员是折翼的天使,猛然发现写了做么多年的Android,竟然不知 ...
- python 常用的模块(struct)转
准确地讲,Python没有专门处理字节的数据类型.但由于str既是字符串,又可以表示字节,所以,字节数组=str.而在C语言中,我们可以很方便地用struct.union来处理字节,以及字节和int, ...
- GLSL实现Glow效果 【转】
http://blog.csdn.net/a3070173/article/details/3220940 Glow即辉光效果现在已成为3D图形中一个引人注目的特效.本文主要介绍如何使用GLSL实现一 ...