最佳答案
我记得,在 Java8之前,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;
}