java 1.8 ArrayList容量增长方式
来源:互联网 发布:ps淘宝9秒视频制作 编辑:程序博客网 时间:2024/06/14 10:56
<span style="font-family: Arial, Helvetica, sans-serif;"> /**</span>
* 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) { int minExpand = (elementData != EMPTY_ELEMENTDATA) // any size if real element table ? 0 // larger than default for empty table. It's already supposed to be // at default size. : DEFAULT_CAPACITY; if (minCapacity > minExpand) { ensureExplicitCapacity(minCapacity); } } private void ensureExplicitCapacity(int minCapacity) { modCount++; // overflow-conscious code if (minCapacity - elementData.length > 0) grow(minCapacity); } /** * 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; /** * Increases the capacity to ensure that it can hold at least the * number of elements specified by the minimum capacity argument. * * @param minCapacity the desired minimum capacity */ private void grow(int minCapacity) { // overflow-conscious code int oldCapacity = elementData.length; int newCapacity = oldCapacity + (oldCapacity >> 1); if (newCapacity - minCapacity < 0) newCapacity = minCapacity; if (newCapacity - MAX_ARRAY_SIZE > 0) newCapacity = hugeCapacity(minCapacity); // minCapacity is usually close to size, so this is a win: elementData = Arrays.copyOf(elementData, newCapacity); } private static int hugeCapacity(int minCapacity) { if (minCapacity < 0) // overflow throw new OutOfMemoryError(); return (minCapacity > MAX_ARRAY_SIZE) ? Integer.MAX_VALUE : MAX_ARRAY_SIZE; }在grow函数中会设置默认新容量为旧容量的1.5倍,若新容量仍小于传入的参数,则设置新容量为入参,同时会判断这个新容量是否太大,若太大则会用hugeCapacity函数进行调整。最后会调用Arrays.copyOf函数将旧数组内的元素复制到一个新数组。
0 0
- java 1.8 ArrayList容量增长方式
- ArrayList集合容量增长
- jdk 1.7中 ArrayList的容量增长方式
- java 1.7之后的Arraylist容量增长机制
- ArrayList动态增长方式
- ArrayList长度增长方式
- JAVA -- arraylist 容量
- c++ vector的容量增长方式
- 关于ArrayList的容量的增长的问题研究
- Java中的ArrayList的容量
- Java中的ArrayList的容量
- Java中的ArrayList的初始容量和容量分配
- Java中的ArrayList的初始容量和容量分配
- java中ArrayList初始容量和容量分配
- Java中的ArrayList的初始容量和容量分配
- Java中的ArrayList的初始容量和容量分配
- Java中的ArrayList的初始容量和容量分配
- Java中的ArrayList的初始容量和容量分配
- 算法设计☞循环赛
- QModel 显示checkBox
- FreeMarker模板使用方法讲解
- 快速排序, 堆排序,归并排序【N*logN】
- Andoird Studio 错误: 非法字符: '\ufeff' 解决方案。
- java 1.8 ArrayList容量增长方式
- dubbo + zookeeper + spring
- android自定义view之自定义容器
- 内存存储格式打印
- android 4.4 系统永不休眠
- 编译 Linux 3.5 内核烧写 Android 到tiny4412
- SpringMVC+spring-security+sitemesh+hibernate+freemarker整合
- 信号(SIGNAL)与槽(SLOT)
- HBase基础讲解