You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Each ArrayList instance has a capacity. The capacity is the size of the array used to store the elements in the list. It is always at least as large as the list size. As elements are added to an ArrayList, its capacity grows automatically. The details of the growth policy are not specified beyond the fact that adding an element has constant amortized time cost.
/** * 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 */privatestaticfinalintMAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
...
// 获取容量时会进行比较,最大值定为Integer.MAX_VALUEprivatestaticinthugeCapacity(intminCapacity) {
if (minCapacity < 0) // overflowthrownewOutOfMemoryError();
return (minCapacity > MAX_ARRAY_SIZE) ?
Integer.MAX_VALUE :
MAX_ARRAY_SIZE;
}
...
三个类的继承关系图
ArrayList
LinkedList
Vector
引伸思考的问题
运行中的java如何查看每个变量,的内存开销?
java运行内存开销是否可以实时查看?
参考文献
https://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html
https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html
https://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html
https://zhuanlan.zhihu.com/p/34828859
The text was updated successfully, but these errors were encountered: