一、ArrayList简介
ArrayList,也就是我们常说的动态数组。其实也就是Array的复杂版本,不同的是它提供了动态地增加和减少数据。
每个ArrayList实例都有一个容量,该容量是指用来存储列表元素的数组的大小。它总是至少等于列表的大小。随着向ArrayList中不断添加元素,其容量也自动增长。自动增长会带来数据向新数组的重新拷贝,因此,如果可预知数据量的多少,可在构造ArrayList时指定其容量。
另外需要提及的一点就是,ArrayList并不是线程安全的,一般建议在单线程中使用ArrayList。
理解集合,最好的方式就是结合它的结构和源码来理解。
ArrayList的结构大概是这样
它是一个动态数组。它的容量capacity一般是会比存储的元素的长度大的。
那么我们接下来看ArrayList的源码。
二、ArrayList源码
存储实现
实际上ArrayList中的元素是存储在ArrayList中的 elementData 中的。底层使用数组实现。
private transient Object[] elementData;
构造函数
ArrayList提供了三种方式的构造函数
构造一个默认初始容量为10的空列表。
public ArrayList() { this(10); }
构造一个指定初始容量的空列表。
public ArrayList(int initialCapacity) { super(); if (initialCapacity < 0) throw new IllegalArgumentException("Illegal Capacity: "+ initialCapacity); this.elementData = new Object[initialCapacity]; }
构造一个包含指定Collection的元素的列表,这些元素按照该Collection的迭代器返回它们的顺序排列。
public ArrayList(Collection<? extends E> c) { elementData = c.toArray(); size = elementData.length; // c.toArray might (incorrectly) not return Object[] (see 6260652) if (elementData.getClass() != Object[].class) elementData = Arrays.copyOf(elementData, size, Object[].class); }
存储数据
ArrayList提供了set(int index, E element)、add(E e)、add(int index, E element)、addAll(Collection<? extends E> c)、addAll(int index, Collection<? extends E> c)这些添加元素的方法。
set(int index,Eelement)
public E set(int index, E element) { RangeCheck(index); E oldValue = (E) elementData[index]; elementData[index] = element; return oldValue; }
//用指定的元素替代此列表中指定位置上的元素,并返回以前位于该位置上的元素
add(E e)
public boolean add(E e) { ensureCapacity(size + 1); elementData[size++] = e; return true; } // 将指定的元素添加到此列表的尾部。
add(int index , E element)
// 将指定的元素插入此列表中的指定位置。 // 如果当前位置有元素,则向右移动当前位于该位置的元素以及所有后续元素(将其索引加1)。 public void add(int index, E element) { if (index > size || index < 0) throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size); // 如果数组长度不足,将进行扩容。 ensureCapacity(size+1); // Increments modCount!! // 将 elementData中从Index位置开始、长度为size-index的元素, // 拷贝到从下标为index+1位置开始的新的elementData数组中。 // 即将当前位于该位置的元素以及所有后续元素右移一个位置。 System.arraycopy(elementData, index, elementData, index + 1, size - index); elementData[index] = element; size++; }
从上述方法也可以看出,ArrayList是基于数组实现的,增删改时效率会较低。
addAll(Collection<? extends E> c)
// 按照指定collection的迭代器所返回的元素顺序,将该collection中的所有元素添加到此列表的尾部。 public boolean addAll(Collection<? extends E> c) { Object[] a = c.toArray(); int numNew = a.length; ensureCapacity(size + numNew); // Increments modCount System.arraycopy(a, 0, elementData, size, numNew); size += numNew; return numNew != 0; }
addAll(int index ,Collection<? extends E> c)
// 从指定的位置开始,将指定collection中的所有元素插入到此列表中。 public boolean addAll(int index, Collection<? extends E> c) { if (index > size || index < 0) throw new IndexOutOfBoundsException( "Index: " + index + ", Size: " + size); Object[] a = c.toArray(); int numNew = a.length; ensureCapacity(size + numNew); // Increments modCount int numMoved = size - index; if (numMoved > 0) System.arraycopy(elementData, index, elementData, index + numNew, numMoved); System.arraycopy(a, 0, elementData, index, numNew); size += numNew; return numNew != 0; }
查询元素
get(int index)
// 返回此列表中指定位置上的元素。 public E get(int index) { RangeCheck(index); return (E) elementData[index]; }
删除元素
ArrayList 提供了根据下标或者指定对象两种方式的删除功能
remove(int index)
// 移除此列表中指定位置上的元素。 public E remove(int index) { RangeCheck(index); modCount++; E oldValue = (E) elementData[index]; int numMoved = size - index - 1; if (numMoved > 0) System.arraycopy(elementData, index+1, elementData, index, numMoved); elementData[--size] = ; // Let gc do its work return oldValue; }
remove(Object o)
// 移除此列表中首次出现的指定元素(如果存在)。这是应为ArrayList中允许存放重复的元素。 public boolean remove(Object o) { // 由于ArrayList中允许存放null,因此下面通过两种情况来分别处理。 if (o == null) { for (int index = 0; index < size; index++) if (elementData[index] == null) { // 类似remove(int index),移除列表中指定位置上的元素。 fastRemove(index); return true; } } else { for (int index = 0; index < size; index++) if (o.equals(elementData[index])) { fastRemove(index); return true; } } return false; }
移除元素时,也会导致被移除的元素之后的元素向前移动一个位置。
调整数组容量
从以上添加元素的代码中可以看到,每当添加元素时,都会去检查添加元素后元素的个数是否会超出当前数组的长度,如果超出,数组将会进行扩容,以满足存储数据的需求。数组的扩容是通过ensureCapacity(int minCapacity)来实现。在实际添加大量元素前,我们也可以使用ensureCapacity来手动增加ArrayList实例的容量,减少再分配的次数。
public void ensureCapacity(int minCapacity) { modCount++; //modCount记录着集合的修改次数,可以通过这个来确认线程之间修改的可见性 int oldCapacity = elementData.length; if (minCapacity > oldCapacity) { Object oldData[] = elementData; int newCapacity = (oldCapacity * 3)/2 + 1; if (newCapacity < minCapacity) newCapacity = minCapacity; // minCapacity is usually close to size, so this is a win: elementData = Arrays.copyOf(elementData, newCapacity); } }
Fail-Fast机制:
ArrayList也采用了快速失败的机制,通过记录modCount参数来实现。在面对并发的修改时,迭代器很快就会完全失败,而不是冒着在将来某个不确定时间发生任意不确定行为的风险。
ArrayList的大致内容就是这些了。