Regarding ArrayList and Vector how to increase size

Java Spec does not specify how to implement them, so it is up to JVM provider to implement them. In my opinion, it does not make too much sense to ask how those methods are implemented. Here is the implementations of Sun Java 1.5.0_07

Vector:

private void ensureCapacityHelper(int minCapacity) {
int oldCapacity = elementData.length;
if (minCapacity > oldCapacity) {
Object[] oldData = elementData;
int newCapacity = (capacityIncrement > 0) ?
(oldCapacity + capacityIncrement) : (oldCapacity * 2);
if (newCapacity newCapacity = minCapacity;
}
elementData = new Object[newCapacity];
System.arraycopy(oldData, 0, elementData, 0, elementCount);
}
}

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 newCapacity = minCapacity;
elementData = (E[])new Object[newCapacity];
System.arraycopy(oldData, 0, elementData, 0, size);
}
}

所有跟帖: 

that is too deep, interview won't ask this deep, -eric_in_chicago- 给 eric_in_chicago 发送悄悄话 eric_in_chicago 的博客首页 (130 bytes) () 12/20/2007 postreply 10:34:45

请您先登陆,再发跟帖!