jdk1.6 1.7 list擴容的區別

jdk1.6this

public void ensureCapacity(int minCapacity) {  spa

    modCount++;  code

    int oldCapacity = elementData.length;  ci

    if (minCapacity > oldCapacity) {  element

        Object oldData[] = elementData;  it

    int newCapacity = (oldCapacity * 3)/2 + 1;  io

    if (newCapacity < minCapacity)  效率

        newCapacity = minCapacity;  jdk

    // minCapacity is usually close to size, so this is a win:  static

    elementData = Arrays.copyOf(elementData, newCapacity); 

jdk1.7

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;

    }

區別:一、擴容後大小區別,1.6乘除運算後爲1.5x+1,1.7位運算後爲1.5x,效率提升

           二、1.7設置最大容量限制

相關文章
相關標籤/搜索