java学习:ArrayList的实现及原理

 

1. ArrayList概述: 数组

      ArrayList是List接口的可变数组的实现。实现了全部可选列表操做,并容许包括 null 在内的全部元素。除了实现 List 接口外,此类还提供一些方法来操做内部用来存储列表的数组的大小。每一个ArrayList实例都有一个容量,该容量是指用来存储列表元素的数组的大小。它老是至少等于列表的大小。随着向ArrayList中不断添加元素,其容量也自动增加。自动增加会带来数据向新数组的从新拷贝,所以,若是可预知数据量的多少,可在构造ArrayList时指定其容量。在添加大量元素前,应用程序也可使用ensureCapacity操做来增长ArrayList实例的容量,这能够减小递增式再分配的数量。 注意,此实现不是同步的。若是多个线程同时访问一个ArrayList实例,而其中至少一个线程从结构上修改了列表,那么它必须保持外部同步。并发

2. ArrayList的实现: this

   对于ArrayList而言,它实现List接口、底层使用数组保存全部元素。其操做基本上是对数组的操做。下面咱们来分析ArrayList的源代码:spa

   1) 底层使用数组实现:线程

1 private transient Object[] elementData;

    2) 构造方法:
   ArrayList提供了三种方式的构造器,能够构造一个默认初始容量为10的空列表、构造一个指定初始容量的空列表以及构造一个包含指定collection的元素的列表,这些元素按照该collection的迭代器返回它们的顺序排列的。code

 1 public ArrayList() {
 2     this(10);
 3 }
 4 
 5 public ArrayList(int initialCapacity) {
 6     super();
 7     if (initialCapacity < 0)
 8         throw new IllegalArgumentException("Illegal Capacity: "+ initialCapacity);
 9     this.elementData = new Object[initialCapacity];
10 }
11 
12 public ArrayList(Collection<? extends E> c) {
13     elementData = c.toArray();
14     size = elementData.length;
15     // c.toArray might (incorrectly) not return Object[] (see 6260652)
16     if (elementData.getClass() != Object[].class)
17         elementData = Arrays.copyOf(elementData, size, Object[].class);
18 }

 

    3) 存储:
   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)这些添加元素的方法。下面咱们一一讲解:
// 用指定的元素替代此列表中指定位置上的元素,并返回之前位于该位置上的元素。
public E set(int index, E element) {
    RangeCheck(index);

    E oldValue = (E) elementData[index];
    elementData[index] = element;
    return oldValue;
}
// 将指定的元素添加到此列表的尾部。   
public boolean add(E e) {   
    ensureCapacity(size + 1);    
    elementData[size++] = e;   
    return true;   
}  
// 将指定的元素插入此列表中的指定位置。
// 若是当前位置有元素,则向右移动当前位于该位置的元素以及全部后续元素(将其索引加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++;
}
// 按照指定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;
}
// 从指定的位置开始,将指定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;
}

    4) 读取:blog

// 返回此列表中指定位置上的元素。
public E get(int index) {
    RangeCheck(index);

    return (E) elementData[index];
}

 

    5) 删除: 
// 移除此列表中指定位置上的元素。
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] = null; // Let gc do its work

    return oldValue;
}
// 移除此列表中首次出现的指定元素(若是存在)。这是应为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;
}

 

     注意:从数组中移除元素的操做,也会致使被移除的元素之后的全部元素的向左移动一个位置。
   6) 调整数组容量:
   从上面介绍的向ArrayList中存储元素的代码中,咱们看到,每当向数组中添加元素时,都要去检查添加后元素的个数是否会超出当前数组的长度,若是超出,数组将会进行扩容,以知足添加数据的需求。数组扩容经过一个公开的方法ensureCapacity(int minCapacity)来实现。在实际添加大量元素前,我也可使用ensureCapacity来手动增长ArrayList实例的容量,以减小递增式再分配的数量。
public void ensureCapacity(int minCapacity) {
    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);
    }
}

 

   从上述代码中能够看出,数组进行扩容时,会将老数组中的元素从新拷贝一份到新的数组中,每次数组容量的增加大约是其原容量的1.5倍。这种操做的代价是很高的,所以在实际使用时,咱们应该尽可能避免数组容量的扩张。当咱们可预知要保存的元素的多少时,要在构造ArrayList实例时,就指定其容量,以免数组扩容的发生。或者根据实际需求,经过调用ensureCapacity方法来手动增长ArrayList实例的容量。
   ArrayList还给咱们提供了将底层数组的容量调整为当前列表保存的实际元素的大小的功能。它能够经过trimToSize方法来实现。代码以下:
public void trimToSize() {
    modCount++;
    int oldCapacity = elementData.length;
    if (size < oldCapacity) {
        elementData = Arrays.copyOf(elementData, size);
    }
}

   7) Fail-Fast机制:
ArrayList也采用了快速失败的机制,经过记录modCount参数来实现。在面对并发的修改时,迭代器很快就会彻底失败,而不是冒着在未来某个不肯定时间发生任意不肯定行为的风险。 索引

相关文章
相关标签/搜索