JDK源码--ArrayList浅析

先上别人的源码分析http://www.cnblogs.com/roucheng/p/jdkfenxi.html

这个链接也不错:http://www.jianshu.com/p/8d14b55fa1fb

具体需要注意的几点:

  1、默认new ArrayList()时创建一个长度为0的数组。当添加新元素的时候,如果是这种方式添加的则直接将数组长度扩展到10。

  2、数组为null和空数组new Object[0]的区别是:假设一个方法返回一个数组,如果它返回null,则调用方法必须先判断是否返回null,才能对放回数组进一步处理,而如果返回空数组,则无须null引用检查。鉴于此,返回数组的方法在没有结果时我们通常返回空数组,而不是null,这样做对于函数调用者的处理比较方便。

  

  private static final Object[] EMPTY_ELEMENTDATA = {};
  private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};

  3、数组扩容的方法:默认直接增长到10。当需要增加到的容量小于原来容量的1.5倍时,继续增长到原来容量的1.5倍,否则增加到minCapacity。当容量太大超过Integer.MAX_VALUE - 8,则增加到Integer.MAX_VALUE。溢出则报异常。

private void ensureCapacityInternal(int minCapacity) {
        if (elementData == DEFAULTCAPACITY_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;
    }

  add方法会首先调用ensureCapacityInternal()扩容,然后在在最后位置将数据写入。

public boolean add(E e) {
        ensureCapacityInternal(size + 1);  // Increments modCount!!
        elementData[size++] = e;
        return true;
    }
Arrays.copyOf(elementData, newCapacity)
最后调用
public static native void arraycopy(Object src,  int  srcPos,
                                        Object dest, int destPos,
                                        int length)

  使用本地接口,在C/C++里完成数组复制扩充

4、toArray方法:两个重载的方法。第二个方法传入一个数组,这个数组会放入list的值,

  如list中原来存放的每个元素都是char[] 数组,则通过传入char[][]后,

    char[][] a =(char[][])list.toArray(new char[list.size()][]), 

  返回的就是char[][]

public Object[] toArray() {
  return Arrays.copyOf(elementData, size);
}

@SuppressWarnings("unchecked")
public <T> T[] toArray(T[] a) {
  if (a.length < size)
  // Make a new array of a's runtime type, but my contents:
  return (T[]) Arrays.copyOf(elementData, size, a.getClass());
  System.arraycopy(elementData, 0, a, 0, size);
  if (a.length > size)
    a[size] = null;
  return a;
}
原文地址:https://www.cnblogs.com/ljdblog/p/5937959.html