st中的一些小细节

st中的一些小细节

ArrayList中的一些小细节@JDK8

protected transient int modCount = 0;

当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.
*/

中文翻译:

还是不太明了

接着往下看:

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

去除空闲的区域

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