查看JDK1.8 ArrayList的源代码:
1、默认初始容量为10
/**
* Default initial capacity.
*/
private static final int DEFAULT_CAPACITY = 10;
2、最大容量为 Integer.MAX_VALUE - 8
/**
* The maximum size of array to allocate.
* Some VMs reserve some header words in an array.
* Attempts to allocate larger arrays may result in
* OutOfMemoryError: Requested array size exceeds VM limit
*/
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
原因:之前参考别人的,有待求证:
数组对象有一个额外的元数据,用于表示数组的大小;
数组长度size为int类型,共32位,有一位符号位,所以最大长度为Integer.MAX_VALUE=2^31= 2,147,483,648;
8bytes用来存储size;
3、扩容方式: