ArrayList中的一些小细节@JDK8

ArrayList中的一些小细节@JDK8

protected transient int modCount = 0;

该变量用于记录ArrayList的版本号,不可被序列化,每次对ArrayList操作都会修改此版本号,为ArrayList提供FastFail功能;可是,在每次操作中都操作此变量,会造成一个结果就是该变量会迅速变化,很快超过Integer.MAXVALUE;那么,当其发生溢出时会不会就不正确了呢,答案时否;

当int发生整形溢出的时候会从重新开始,这样保证了在有限的操作次数内,对于不同操作的版本号一定会不相同

int k = Integer.MAX_VALUE;
for (; ; ) {
    System.out.println(k++);
    try {
        Thread.sleep(1000);
    } catch (Exception e) {

    }
}

输出:


private static final Object[] EMPTY_ELEMENTDATA = {};

private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};

两个居然一模一样

查看一下注释

/**
 * Shared empty array instance used for default sized empty instances. We
 * distinguish this from EMPTY_ELEMENTDATA to know how much to inflate when
 * first element is added.
 */

中文翻译:

用于默认大小的空实例的共享空数组实例。我们将其与EMPTY_ELEMENTDATA区分开来,以了解在添加第一个元素时要膨胀多少。

还是不太明了

接着往下看:

remove方法:

public boolean remove(Object o) {
    if (o == null) {
        for (int index = 0; index < size; index++)
            if (elementData[index] == null) {
            	//仅仅移除第一个对象
                fastRemove(index);
                return true;
            }
    } else {
        for (int index = 0; index < size; index++)
            if (o.equals(elementData[index])) {
                //仅仅移除第一个对象
                fastRemove(index);
                return true;
            }
    }
    return false;
}

fastRemove方法:

private void fastRemove(int index) {
    modCount++;
    int numMoved = size - index - 1;
    if (numMoved > 0)
        System.arraycopy(elementData, index+1, elementData, index,
                         numMoved);
    elementData[--size] = null; // clear to let GC do its work
}

grow方法

    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);
    }

不足够时,默认增长0.5倍,如果还是小于最小长度就赋予最小长度minCapacity=10

然后扩充

去除空闲的区域

public void trimToSize() {
    modCount++;
    if (size < elementData.length) {
        elementData = (size == 0)
          ? EMPTY_ELEMENTDATA
          : Arrays.copyOf(elementData, size);
    }
}

因为有trimToSize()所以才有存在capacity<minCapacity的情况

posted @ 2019-03-13 18:02  随风而行-  阅读(270)  评论(0编辑  收藏  举报