1. 数据结构--ArrayList源码摘要

ublic class ArrayList<E> extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable
{
private static final long serialVersionUID = 8683452581122892189L; /**
* Default initial capacity.
*/
private static final int DEFAULT_CAPACITY = 10; /**
* Shared empty array instance used for empty instances.
*/
private static final Object[] EMPTY_ELEMENTDATA = {}; /**
* The array buffer into which the elements of the ArrayList are stored.
* The capacity of the ArrayList is the length of this array buffer. Any
* empty ArrayList with elementData == EMPTY_ELEMENTDATA will be expanded to
* DEFAULT_CAPACITY when the first element is added.
*/
private transient Object[] elementData; /**
* The size of the ArrayList (the number of elements it contains).
*
* @serial
*/
private int size;
}

ArrayList 的底层最重要的两个属性:Object 数组和 size 属性。

2. ArrayList 的底层数组的调整

add方法--ArrayList源码摘要

/**
* Appends the specified element to the end of this list.
*
* @param e element to be appended to this list
* @return <tt>true</tt> (as specified by {@link Collection#add})
*/
public boolean add(E e) {
ensureCapacityInternal(size + 1); // Increments modCount!!
elementData[size++] = e;
return true;
}

grow方法--ArrayList源码摘要


/**


* Increases the capacity of this <tt>ArrayList</tt> instance, if


* necessary, to ensure that it can hold at least the number of elements


* specified by the minimum capacity argument.


*


* @param   minCapacity   the desired minimum capacity


*/


public void ensureCapacity(int minCapacity) {


int minExpand = (elementData != EMPTY_ELEMENTDATA)


// any size if real element table


? 0


// larger than default for empty table. It's already supposed to be


// at default size.


: DEFAULT_CAPACITY;



if (minCapacity > minExpand) {


ensureExplicitCapacity(minCapacity);


}


}



private void ensureCapacityInternal(int minCapacity) {


if (elementData == EMPTY_ELEMENTDATA) {


minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);


}



ensureExplicitCapacity(minCapacity);


}



private void ensureExplicitCapacity(int minCapacity) {


modCount++;



// overflow-conscious code


if (minCapacity - elementData.length > 0)


grow(minCapacity);


}



/**


* The maximum size of array to allocate.


* Some VMs reserve some header words in an array.


* Attempts to allocate larger arrays may result in


* OutOfMemoryError: Requested array size exceeds VM limit


*/


private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;



/**


* 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) {


// overflow-conscious code


int oldCapacity = elementData.length;


int newCapacity = oldCapacity + (oldCapacity >> 1);


if (newCapacity - minCapacity < 0)


newCapacity = minCapacity;


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);


}



private static int hugeCapacity(int minCapacity) {


if (minCapacity < 0) // overflow


throw new OutOfMemoryError();


return (minCapacity > MAX_ARRAY_SIZE) ?


Integer.MAX_VALUE :


MAX_ARRAY_SIZE;


}

 

2点结论:

a. ArrayList 是通过将底层 Object 数组复制的方式(System.arraycopy方法)来处理数组的增长;

b. 当ArrayList 的容量不足时,其扩充容量的方式:先将容量扩充至当前容量的1.5倍,若还不够,则将容量扩充至当前需要的数量(grow方法)。

remove 方法--ArrayList源码摘要

/**
* 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; // clear to 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


* <tt>i</tt> such that


* <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt>


* (if such an element exists).  Returns <tt>true</tt> 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 <tt>true</tt> 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;


}



/*


* 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; // clear to let GC do its work


}

 

这也就解释了 ArrayList 的特点:增加、删除和移动元素的效率低(数组复制过程消耗资源较多); 而查找元素和更新元素的效率高。

3. ArrayList与Vector的区别

1) vector 是线程同步的,所以它也是线程安全的,而arraylist 是线程异步的,是不安全的。如果不考虑到线程的安全因素,一般用 arraylist效率比较高。

2)如果集合中的元素的数目大于目前集合数组的长度时,vector 增长率为目前数组长度的100%, 而arraylist 增长率为目前数组长度的50% .如果在集合中使用数据量比较大的数据,用vector有一定的优势。

3)如果查找一个指定位置的数据,vector和arraylist使用的时间是相同的,都是O(1) ,这个时候使用vector和arraylist都可以。

而如果移动一个指定位置的数据花费的时间为O(n-i)n为总长度,这个时候就应该考虑到使用linklist ,因为它移动一个指定位置的数据所花费的时间为0(1),而查询一个指定位置的数据时花费的时间为0(i)。

Java 的 ArrayList 的底层数据结构的更多相关文章

  1. 词典的实现(3)--使用JAVA类库ArrayList实现Map数据结构

    1,在词典的实现(2)-借助顺序表(数组)实现词典文章中使用了自定义的数组代替ArrayList,并实现了Map数据结构的基本功能.而借助JAVA类库ArrayList类的一些方法可以更加容易地实现M ...

  2. Java 的 LinkedList 的底层数据结构

    1. 数据结构--LinkedList源码摘要 public class LinkedList<E> extends AbstractSequentialList<E> imp ...

  3. Java集合——ArrayList源码详解

    ) ArrayList 实现了RandomAccess, Cloneable, java.io.Serializable三个标记接口,表示它自身支持快速随机访问,克隆,序列化. public clas ...

  4. Java中ArrayList和LinkedList的性能分析

    ArrayList和LinkedList是Java集合框架中经常使用的类.如果你只知道从基本性能比较ArrayList和LinkedList,那么请仔细阅读这篇文章. ArrayList应该在需要更多 ...

  5. ArrayList的底层实现原理

    ArrayList源码分析 1.java.util.ArrayList<E> : List 接口的大小可变数组的实现类 ArrayList 内部基于 数组 存储 各个元素. 所谓大小可变数 ...

  6. Java 8 ArrayList 详解

    GitHub Page: http://blog.cloudli.top/posts/Java-ArrayList/ ArrayList 继承于 AbstractList ,实现了 List.Rand ...

  7. Redis学习笔记(二)redis 底层数据结构

    在上一节提到的图中,我们知道,可以通过 redisObject 对象的 type 和 encoding 属性.可以决定Redis 主要的底层数据结构:SDS.QuickList.ZipList.Has ...

  8. 深入解析Java对象的hashCode和hashCode在HashMap的底层数据结构的应用

    转自:http://kakajw.iteye.com/blog/935226 一.java对象的比较 等号(==): 对比对象实例的内存地址(也即对象实例的ID),来判断是否是同一对象实例:又可以说是 ...

  9. [转]java 的HashMap底层数据结构

    java 的HashMap底层数据结构   HashMap也是我们使用非常多的Collection,它是基于哈希表的 Map 接口的实现,以key-value的形式存在.在HashMap中,key-v ...

随机推荐

  1. shell中获取文件目录方法

    1.``:表示执行对应的命令,嵌套时使用`\`\``,注意\进行转义,同时执行多个命令时使用:隔开file=`cd "\`dirname $0\`";pwd`echo $file ...

  2. JavaScript—面向对象贪吃蛇_1

    前面说了.面向对象的思考方式和面向过程的思考方式有着本质的区别. 贪吃蛇.作为各大培训机构.面向对象的练手项目,的确好.我昨天看完视频,有一种领悟面向对象的感觉,当然可能只针对贪吃蛇..要想在实际开发 ...

  3. 萤火虫系统(firefly) RK3399 python3 安装 spicy

    最近一直在研究深度学习,公司里正好也有个项目是需要在RK3399的板子上跑人脸识别. 原本在linux_x86系统上轻轻松松装的环境到了RK3399板子上(firefly系统)安装起来真的非常麻烦,需 ...

  4. Java8之深入理解Lambda

    lambda表达式实战 从例子引出lambda 传递Runnable创建Thread java8之前 Thread thread=new Thread(new Runnable() { @Overri ...

  5. 微服务项目开发学成在线_Vue.js与Webpack

    Vue.js 1.Vue (读音 /vjuː/,类似于 view) 是一套用于构建用户界面的渐进式框架.自底向上逐层应用:作为渐进式框架要实现的目标就是方便项目增量开发. 渐进式框架:Progress ...

  6. Python筛法求素数

    l=[2]m,n=input().split()m=int(m)n=int(n) for i in range(m,n): flag=True for j in l: if i%j==0:#如果当前值 ...

  7. Tensorflow学习教程------模型参数和网络结构保存且载入,输入一张手写数字图片判断是几

    首先是模型参数和网络结构的保存 #coding:utf-8 import tensorflow as tf from tensorflow.examples.tutorials.mnist impor ...

  8. Linux-进程状态和system函数

    1.进程的5种状态 (1).就绪态. 这个进程当前所有运行条件就绪,只要得到CPU时间就能直接运行. (2).运行态 就绪态时得到了CPU就进入运行态开始运行. (3).僵尸态 进程已经结束但是父进程 ...

  9. Mysql存储过程案例集合

    注:使用的工具为SQLyog 壹.while简单使用(替换字符串中的字符,和REPLACE效果一样) 注: 这里没有使用REPLACE函数 1.创建存储过程 DROP PROCEDURE IF EXI ...

  10. 7.学完linux系统运维到底可以做什么?

    linux运维到底可以做什么?(略有改动原文.排版) 运维,很容易从字面理解为运营.维护. 很多朋友认为,在互联网公司中linux系统运维的工作就是安装系统,部署服务.处理紧急故障,为公司里的开发人员 ...