【发布时间】:2016-03-18 22:34:24
【问题描述】:
我记得在 Java 8 之前,ArrayList 的默认容量是 10。
令人惊讶的是,默认(void)构造函数的注释仍然显示:Constructs an empty list with an initial capacity of ten.
来自ArrayList.java:
/**
* 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.
*/
private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
...
/**
* Constructs an empty list with an initial capacity of ten.
*/
public ArrayList() {
this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
}
【问题讨论】: