博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ArrayList
阅读量:6692 次
发布时间:2019-06-25

本文共 3889 字,大约阅读时间需要 12 分钟。

ArrayList概述:

ArrayList是实现List接口的,大小可变数组的实现。实现了所有可选列表操作,并允许包括 null 在内的所有元素。除了实现 List 接口外,此类还提供一些方法来操作内部用来存储列表的数组的大小

ArrayList的实现:

/**     * The array buffer into which the elements of the ArrayList are stored.     * The capacity of the ArrayList is the length of this array buffer.     */    private transient Object[] elementData;    /**     * The size of the ArrayList (the number of elements it contains).     *     * @serial     */    private int size;

elementData存储ArrayList中的元素,size表示包含元素的数量。让我们来看下ArrayList内部数组是如何自我Copy的。

Add方法:

/**     * Appends the specified element to the end of this list.     *     * @param e element to be appended to this list     * @return true (as specified by {
@link Collection#add}) */ public boolean add(E e) { ensureCapacityInternal(size + 1); // Increments modCount!! elementData[size++] = e; return true; } /** * Inserts the specified element at the specified position in this * list. Shifts the element currently at that position (if any) and * any subsequent elements to the right (adds one to their indices). * * @param index index at which the specified element is to be inserted * @param element element to be inserted * @throws IndexOutOfBoundsException {
@inheritDoc} */ public void add(int index, E element) { rangeCheckForAdd(index); ensureCapacityInternal(size + 1); // Increments modCount!! System.arraycopy(elementData, index, elementData, index + 1, size - index); elementData[index] = element; size++; }

Remove方法:

/**     * Removes the element at the specified position in this list.     * Shifts any subsequent elements to the left (subtracts one from their     * indices).     *     * @param index the index of the element to be removed     * @return the element that was removed from the list     * @throws IndexOutOfBoundsException {
@inheritDoc} */ public E remove(int index) { rangeCheck(index); modCount++; E oldValue = elementData(index); int numMoved = size - index - 1; if (numMoved > 0) System.arraycopy(elementData, index+1, elementData, index, numMoved); elementData[--size] = null; // Let gc do its work return oldValue; } /** * Removes the first occurrence of the specified element from this list, * if it is present. If the list does not contain the element, it is * unchanged. More formally, removes the element with the lowest index * i such that * (o==null ? get(i)==null : o.equals(get(i))) * (if such an element exists). Returns true if this list * contained the specified element (or equivalently, if this list * changed as a result of the call). * * @param o element to be removed from this list, if present * @return true if this list contained the specified element */ public boolean remove(Object o) { if (o == null) { for (int index = 0; index < size; index++) if (elementData[index] == null) { fastRemove(index); return true; } } else { for (int index = 0; index < size; index++) if (o.equals(elementData[index])) { fastRemove(index); return true; } } return false; }

从以上可以看出执行的过程就是一个数组的复制(System.arraycopy)

System.arraycopy:

首先我们来看看System.arraycopy的声明:

public static native void arraycopy(Object src,  int  srcPos,                                        Object dest, int destPos,                                        int length);

src:源数组

srcPos:源数组中的起始位置

dest:目标数组

destPos:目标数组中的起始位置

length:要复制数量

该方法被标记了native,调用了系统的C/C++代码,在JDK中是看不到的。在复制大量数组元素时用该方法,以取得更高的效率。

转载于:https://www.cnblogs.com/luojianguang/p/6201869.html

你可能感兴趣的文章
2016第42周五
查看>>
centos7 取消自动锁屏
查看>>
在IDEA中代码自动提示第一个字母大小写必须匹配的解决
查看>>
C++的字符串格式化库
查看>>
面向接口编程的好处和优点
查看>>
放过设计模式吧
查看>>
架构师必看-架构之美第14章-两个系统的故事:设计之城(一)
查看>>
从c++转到Python需要注意的地方
查看>>
mysql 利用触发器(Trigger)让代码更简单
查看>>
[译]ASP.NET Core 2.0 视图引擎
查看>>
(原)InsightFace及其mxnet代码
查看>>
OpenCV学习:实现简单的图像叠加
查看>>
Intent跳转到系统应用中的拨号界面、联系人界面、短信界面及其他
查看>>
java.io包的总体框架图(转)
查看>>
VKDevTool App黑盒调试工具
查看>>
那个你身边悄悄离职的人去哪儿了?IT人才流动大盘点
查看>>
精读《手写 SQL 编译器 - 智能提示》
查看>>
Java多线程干货系列—(四)volatile关键字| 掘金技术征文
查看>>
我们来翻翻元素样式的族谱-getComputedStyle
查看>>
Hessian HTTP POST访问时,Nginx返回411问题
查看>>