ArrayList概述

ArrayList继承了AbstractList,实现了List接口,底层基于动态数组,容量大小可以动态变化,ArrayList中可以添加null元素,另外,ArrayList是非线程安全的

ArrayList实现List, RandomAccess, Cloneable, java.io.Serializable接口。

  • 实现RandomAccess接口,即标识着该类支持快速随机访问
  • 实现Cloneable接口,即覆盖了clone(),能被克隆
  • 实现Serializable接口,即ArrayList支持序列化,能被序列化去传输

ArrayList源码分析(基于jdk1.8)


成员变量

    /**
* 默认容量为10
*/
private static final int DEFAULT_CAPACITY = 10;
/**
*存储ArrayList元素的数组缓冲区,ArrayList的容量是此数组缓冲区的长度。
*添加第一个元素时,任何带有elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA的空ArrayList都将扩展为默认容量
*/
transient Object[] elementData;
/**
* ArrayList实际的元素个数
*/
private int size;

构造方法

    /**
构造一个容量大小为10的空list,实际在第一次添加元素时才扩容到10
*/
public ArrayList() {
this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
}
/**
* 构造具有指定初始容量的空的list
*/
public ArrayList(int initialCapacity) {
if (initialCapacity > 0) {
this.elementData = new Object[initialCapacity];
} else if (initialCapacity == 0) {
this.elementData = EMPTY_ELEMENTDATA;
} else {
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
}
} //构造一个包含指定 collection 的元素的列表,这些元素是按照该 collection 的迭代器返回它们的顺序排列的
public ArrayList(Collection<? extends E> c) {
elementData = c.toArray();
if ((size = elementData.length) != 0) {
// c.toArray might (incorrectly) not return Object[] (see 6260652)
if (elementData.getClass() != Object[].class)
elementData = Arrays.copyOf(elementData, size, Object[].class);
} else {
// replace with empty array.
this.elementData = EMPTY_ELEMENTDATA;
}
}

添加操作

    public boolean add(E e) {
ensureCapacityInternal(size + 1); // Increments modCount!!
elementData[size++] = e;
return true;
}
private void ensureCapacityInternal(int minCapacity) {
ensureExplicitCapacity(calculateCapacity(elementData, minCapacity));
} private void ensureExplicitCapacity(int minCapacity) {
modCount++;
// overflow-conscious code
if (minCapacity - elementData.length > 0)
grow(minCapacity);
}

扩容机制

//grow()方法是ArrayList扩容最终调用的代码
private void grow(int minCapacity) {
int oldCapacity = elementData.length;
//旧容量向右移1位,效果等同 oldCapacity*0.5
//新的容量是旧容量的1.5倍
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);
}

总结

构造一个空的,不指定容量 ArrayList list = new ArrayList()的list时

当第一次调用add()方法向集合中添加元素时,才会扩容到10。

jdk1.6中ArrayList的扩容机制

  • jdk1.6中是扩容成原来的1.5倍+1,代码: int newCapacity = (oldCapacity * 3)/2 + 1
  • jdk1.7和jdk1.8都是扩容成原来的1.5倍

ArrayList扩容后是原来的数组还是新的数组

ArrayList扩容后是新的数组,当容量不够时,ArrayList底层通过Arrays.copyOf()方法将原来的数组拷贝到新的数组。因此,数据量多时,非常耗时。

Arrays.copyOf()内部调用System.arraycopy()方法

补充

ArrayList和Vector区别

  1. 线程安全:Vector的方法都是同步的,是线程安全的,而ArrayList不是线程安全的。由于同步会影响性能,所以,ArrayList的性能比Vector更好一些。
  2. 扩容机制:ArrayList每次扩容是其大小的1.5倍,而Vector是其大小的2倍。

ArrayList和LinkedList区别

ArrayList和LinkedList都是实现List接口的容器类

  1. 底层数据结构:ArrayList是基于动态数组的数据结构,而LinkedList是基于双向链表的数据结构。
  2. 随机访问效率:对于随机访问get和set,ArrayList要优于LinkedList,因为LinkedList要移动指针。
  3. 空间花费:ArrayList的空间浪费主要体现在list列表的结尾会预留一定的容量空间,而LinkedList的空间花费则体现在它的每一个元素都需要消耗比ArrayList更多的空间(因为要存放直接后继和直接前驱以及数据)。
  4. 是否线程安全: ArrayList 和 LinkedList 都是不同步的,即不保证线程安全。

参考

ArrayList和LinkedList异同

Java集合--LinkedList源码分析

https://blog.51cto.com/sihai/2073367

最新文章

  1. c3p0连接数据库的3种方式
  2. iOS 升级HTTPS通过ATS你所要知道的
  3. java性能调优及问题追踪--Btrace的使用
  4. 【原创】kafka producer源代码分析
  5. 【Beta版本】冲刺-Day3
  6. 《Java核心技术卷一》笔记 多线程
  7. iOS -Swift 3.0 -UIButton属性大全
  8. window上Python环境的搭建
  9. [HDOJ2586]How far away?(最近公共祖先, 离线tarjan, 并查集)
  10. 二分图最大匹配 hdoj 1045
  11. HDU 1086:You can Solve a Geometry Problem too
  12. 关于服务器跨域问题(使用cors解决)
  13. python 导出数据到excel 中,一个好用的导出数据到excel模块,XlsxWriter
  14. MS-DOS运行java工程
  15. spring-data-redis HashOperations
  16. MySQL内连接(INNER JOIN)
  17. 安装ipython和jupyter
  18. (98)Address already in use: make_sock: could not bind to address 80 [resolved] (2012-10-11 09:04)
  19. spring mvc 资源映射配置
  20. Linux - TCP编程相关配置2

热门文章

  1. PHP 微信公众号开发 - 消息推送
  2. C#使用Log4Net记录日志(转)
  3. ubuntu12.04安装nox-classic
  4. awk基础05-自定义函数和脚本
  5. iperf命令 +speedtest-cli
  6. 谁说他们版本不兼容——hadoop1.2.1+hbase0.94.11+nutch2.2.1+el
  7. JS控制输入框,输入正确的价格
  8. maven多模块启动required a bean of type com.xxx.xxx.service that could not be found.
  9. C#语言各个版本特性(二)
  10. 记录一下显示Map&lt;String, ArrayList&lt;String&gt;&gt;中的ArrayList里的数据的操作