当前位置:   article > 正文

Java集合篇:ArrayList详解

arraylist

一、ArrayList概述:

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

每个ArrayList实例都有一个容量,该容量是指用来存储列表元素的数组的大小。默认初始容量是10。默认初始容量为10。随着ArrayList中元素的增加,它的容量也会不断的自动增长。在每次添加元素时,ArrayList都会检查是否需要进行扩容操作,扩容操作带来数据向新数组的重新拷贝,所以如果我们知道具体业务数据量,在构造ArrayList时,可以给ArrayList 指定一个初始容量,这样就会减少扩容时的拷贝问题。当然在添加大量元素前,应用程序也可以使用ensureCapacity操作来增加ArrayList实例的容量,这可以减少递增式再分配的数量。

ArrayList 的实现不是同步的如果多个线程同时访问一个ArrayList实例,而其中至少一个线程从结构上修改了列表,那么它必须保持外部同步。所以为了保证同步,最好的办法是在创建时完成,以防止意外对列表进行不同步的访问:

List list = Collections.synchronizedList(new ArrayList(...));

 

二、ArrayList源码分析:

ArrayList是实现List接口的,底层采用数组实现,所以它的操作基本上都是基于对数组的操作。

1、底层使用数组:

private transient Object[] elementData;

transient 为java关键字,为变量修饰符,如果用transient声明一个实例变量,当对象存储时,它的值不需要维持。Java的serialization提供了一种持久化对象实例的机制。当持久化对象时,可能有一个特殊的对象数据成员,我们不想用serialization机制来保存它。为了在一个特定对象的一个域上关闭serialization,可以在这个域前加上关键字transient。当一个对象被序列化的时候,transient型变量的值不包括在序列化的表示中,然而非transient型的变量是被包括进去的。 

这里Object[] elementData,就是我们的ArrayList容器,下面介绍的基本操作都是基于该elementData变量来进行操作的。

2、构造函数:

ArrayList提供了三个构造函数:

ArrayList():默认构造函数,提供初始容量为10的空列表。

ArrayList(int initialCapacity):构造一个具有指定初始容量的空列表。

ArrayList(Collection<? extends E> c):构造一个包含指定 collection 的元素的列表,这些元素是按照该 collection 的迭代器返回它们的顺序排列的。

  1. /**
  2. * 构造一个初始容量为 10 的空列表
  3. */
  4. public ArrayList() {
  5. this(10);
  6. }
  7. /**
  8. * 构造一个具有指定初始容量的空列表。
  9. */
  10. public ArrayList(int initialCapacity) {
  11. super();
  12. if (initialCapacity < 0)
  13. throw new IllegalArgumentException("Illegal Capacity: "
  14. + initialCapacity);
  15. this.elementData = new Object[initialCapacity];
  16. }
  17. /**
  18. * 构造一个包含指定 collection 的元素的列表,这些元素是按照该 collection 的迭代器返回它们的顺序排列的。
  19. */
  20. public ArrayList(Collection<? extends E> c) {
  21. elementData = c.toArray();
  22. size = elementData.length;
  23. // c.toArray might (incorrectly) not return Object[] (see 6260652)
  24. if (elementData.getClass() != Object[].class)
  25. elementData = Arrays.copyOf(elementData, size, Object[].class);
  26. }

3、新增:

 ArrayList提供了add(E e)、add(int index, E element)、addAll(Collection<? extends E> c)、addAll(int index, Collection<? extends E> c)、set(int index, E element)这个五个方法来实现ArrayList增加。

3.1 add(E e):将指定的元素添加到此列表的尾部。

  1. public boolean add(E e) {
  2. ensureCapacity(size + 1); // Increments modCount!!
  3. elementData[size++] = e;
  4. return true;
  5. }

这里ensureCapacity()方法是对ArrayList集合进行扩容操作,elementData(size++) = e,将列表末尾元素指向e。

 3.2 add(int index, E element):将指定的元素插入此列表中的指定位置。

  1. public void add(int index, E element) {
  2. //判断索引位置是否正确
  3. if (index > size || index < 0)
  4. throw new IndexOutOfBoundsException(
  5. "Index: "+index+", Size: "+size);
  6. //扩容检测
  7. ensureCapacity(size+1);
  8. /*
  9. * 对源数组进行复制处理(位移),从index + 1到size-index。
  10. * 主要目的就是空出index位置供数据插入,
  11. * 即向右移动当前位于该位置的元素以及所有后续元素。
  12. */
  13. System.arraycopy(elementData, index, elementData, index + 1,
  14. size - index);
  15. //在指定位置赋值
  16. elementData[index] = element;
  17. size++;
  18. }

在这个方法中最根本的方法就是System.arraycopy()方法,该方法的根本目的就是将index位置空出来以供新数据插入,这里需要进行数组数据的右移,这是非常麻烦和耗时的,所以如果指定的数据集合需要进行大量插入(中间插入)操作,推荐使用LinkedList。

      3.3 addAll(Collection<? extends E> c):按照指定 collection 的迭代器所返回的元素顺序,将该 collection 中的所有元素添加到此列表的尾部。

  1. public boolean addAll(Collection<? extends E> c) {
  2. // 将集合C转换成数组
  3. Object[] a = c.toArray();
  4. int numNew = a.length;
  5. // 扩容处理,大小为size + numNew
  6. ensureCapacity(size + numNew); // Increments modCount
  7. System.arraycopy(a, 0, elementData, size, numNew);
  8. size += numNew;
  9. return numNew != 0;
  10. }

这个方法无非就是使用System.arraycopy()方法将C集合(先准换为数组)里面的数据复制到elementData数组中。这里就稍微介绍下System.arraycopy(),因为下面还将大量用到该方法。该方法的原型为:public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)。它的根本目的就是进行数组元素的复制。即从指定源数组中复制一个数组,复制从指定的位置开始,到目标数组的指定位置结束。将源数组src从srcPos位置开始复制到dest数组中,复制长度为length,数据从dest的destPos位置开始粘贴。

3.4 addAll(int index, Collection<? extends E> c):从指定的位置开始,将指定 collection 中的所有元素插入到此列表中。

  1. public boolean addAll(int index, Collection<? extends E> c) {
  2. //判断位置是否正确
  3. if (index > size || index < 0)
  4. throw new IndexOutOfBoundsException("Index: " + index + ", Size: "
  5. + size);
  6. //转换成数组
  7. Object[] a = c.toArray();
  8. int numNew = a.length;
  9. //ArrayList容器扩容处理
  10. ensureCapacity(size + numNew); // Increments modCount
  11. //ArrayList容器数组向右移动的位置
  12. int numMoved = size - index;
  13. //如果移动位置大于0,则将ArrayList容器的数据向右移动numMoved个位置,确保增加的数据能够增加
  14. if (numMoved > 0)
  15. System.arraycopy(elementData, index, elementData, index + numNew,
  16. numMoved);
  17. //添加数组
  18. System.arraycopy(a, 0, elementData, index, numNew);
  19. //容器容量变大
  20. size += numNew;
  21. return numNew != 0;
  22. }

3.5 set(int index, E element):用指定的元素替代此列表中指定位置上的元素。

  1. public E set(int index, E element) {
  2. //检测插入的位置是否越界
  3. RangeCheck(index);
  4. E oldValue = (E) elementData[index];
  5. //替代
  6. elementData[index] = element;
  7. return oldValue;
  8. }

4、删除:

ArrayList提供了remove(int index)、remove(Object o)、removeRange(int fromIndex, int toIndex)、removeAll()四个方法进行元素的删除。

4.1 remove(int index):移除此列表中指定位置上的元素。

  1. public E remove(int index) {
  2. //位置验证
  3. RangeCheck(index);
  4. modCount++;
  5. //需要删除的元素
  6. E oldValue = (E) elementData[index];
  7. //向左移的位数
  8. int numMoved = size - index - 1;
  9. //若需要移动,则想左移动numMoved位
  10. if (numMoved > 0)
  11. System.arraycopy(elementData, index + 1, elementData, index,
  12. numMoved);
  13. //置空最后一个元素
  14. elementData[--size] = null; // Let gc do its work
  15. return oldValue;
  16. }

4.2 remove(Object o):移除此列表中首次出现的指定元素(如果存在)。

  1. public boolean remove(Object o) {
  2. //因为ArrayList中允许存在null,所以需要进行null判断
  3. if (o == null) {
  4. for (int index = 0; index < size; index++)
  5. if (elementData[index] == null) {
  6. //移除这个位置的元素
  7. fastRemove(index);
  8. return true;
  9. }
  10. } else {
  11. for (int index = 0; index < size; index++)
  12. if (o.equals(elementData[index])) {
  13. fastRemove(index);
  14. return true;
  15. }
  16. }
  17. return false;
  18. }

其中fastRemove()方法用于移除指定位置的元素。如下:

  1. private void fastRemove(int index) {
  2. modCount++;
  3. int numMoved = size - index - 1;
  4. if (numMoved > 0)
  5. System.arraycopy(elementData, index+1, elementData, index,
  6. numMoved);
  7. elementData[--size] = null; // Let gc do its work
  8. }

4.3 removeRange(int fromIndex, int toIndex):移除列表中索引在 fromIndex(包括)和 toIndex(不包括)之间的所有元素。

  1. protected void removeRange(int fromIndex, int toIndex) {
  2. modCount++;
  3. int numMoved = size - toIndex;
  4. System
  5. .arraycopy(elementData, toIndex, elementData, fromIndex,
  6. numMoved);
  7. // Let gc do its work
  8. int newSize = size - (toIndex - fromIndex);
  9. while (size != newSize)
  10. elementData[--size] = null;
  11. }

4.4 removeAll():是继承自AbstractCollection的方法,ArrayList本身并没有提供实现。

  1. public boolean removeAll(Collection<?> c) {
  2. boolean modified = false;
  3. Iterator<?> e = iterator();
  4. while (e.hasNext()) {
  5. if (c.contains(e.next())) {
  6. e.remove();
  7. modified = true;
  8. }
  9. }
  10. return modified;
  11. }

5、查找:

ArrayList提供了get(int index)用读取ArrayList中的元素。由于ArrayList是动态数组,所以我们完全可以根据下标来获取ArrayList中的元素,而且速度还比较快,故ArrayList长于随机访问。

  1. public E get(int index) {
  2. RangeCheck(index);
  3. return (E) elementData[index];
  4. }

6、扩容:

在上面的新增方法的源码中我们发现每个方法中都存在这个方法:ensureCapacity(),该方法就是ArrayList的扩容方法。在前面就提过ArrayList每次新增元素时都会需要进行容量检测判断,若新增元素后元素的个数会超过ArrayList的容量,就会进行扩容操作来满足新增元素的需求。所以当我们清楚知道业务数据量或者需要插入大量元素前,我可以使用ensureCapacity来手动增加ArrayList实例的容量,以减少递增式再分配的数量。

  1. public void ensureCapacity(int minCapacity) {
  2. //修改计时器
  3. modCount++;
  4. //ArrayList容量大小
  5. int oldCapacity = elementData.length;
  6. /*
  7. * 若当前需要的长度大于当前数组的长度时,进行扩容操作
  8. */
  9. if (minCapacity > oldCapacity) {
  10. Object oldData[] = elementData;
  11. //计算新的容量大小,为当前容量的1.5倍
  12. int newCapacity = (oldCapacity * 3) / 2 + 1;
  13. if (newCapacity < minCapacity)
  14. newCapacity = minCapacity;
  15. //数组拷贝,生成新的数组
  16. elementData = Arrays.copyOf(elementData, newCapacity);
  17. }
  18. }

在这里有一个疑问,为什么每次扩容处理会是1.5倍,而不是2.5、3、4倍呢?通过google查找,发现1.5倍的扩容是最好的倍数。因为一次性扩容太大(例如2.5倍)可能会浪费更多的内存(1.5倍最多浪费33%,而2.5被最多会浪费60%,3.5倍则会浪费71%……)。但是一次性扩容太小,需要多次对数组重新分配内存,对性能消耗比较严重。所以1.5倍刚刚好,既能满足性能需求,也不会造成很大的内存消耗。

处理这个ensureCapacity()这个扩容数组外,ArrayList还给我们提供了将底层数组的容量调整为当前列表保存的实际元素的大小的功能。它可以通过trimToSize()方法来实现。该方法可以最小化ArrayList实例的存储量。

  1. public void trimToSize() {
  2. modCount++;
  3. int oldCapacity = elementData.length;
  4. if (size < oldCapacity) {
  5. elementData = Arrays.copyOf(elementData, size);
  6. }
  7. }

 

文章转自:http://www.cnblogs.com/chenssy/p/3498468.html

 

 

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号