我刚刚偶然发现了 Java 6 中的一行代码,我不清楚它的功能。
它是ArrayList 方法中Object oldData[] = elementData;
的一行。只是看起来是一个局部变量,在方法体的范围内没有任何用途。我在作业中错过了一些隐藏的魔法吗?ensureCapacity(int minCapacity)
oldData
/**
* Increases the capacity of this <tt>ArrayList</tt> instance, if
* necessary, to ensure that it can hold at least the number of elements
* specified by the minimum capacity argument.
*
* @param minCapacity the desired minimum capacity
*/
public void ensureCapacity(int minCapacity) {
modCount++;
int oldCapacity = elementData.length;
if (minCapacity > oldCapacity) {
Object oldData[] = elementData;
int newCapacity = (oldCapacity * 3)/2 + 1;
if (newCapacity < minCapacity)
newCapacity = minCapacity;
// minCapacity is usually close to size, so this is a win:
elementData = Arrays.copyOf(elementData, newCapacity);
}
}