What#39;s meant by parameter (int initial capacity) in an arraylist(数组列表中的参数(int初始容量)是什么意思)
问题描述
ArrayList中的参数(int initialCapacity)是什么意思,我以为是元素的个数,但是这样做的时候不起作用:
What's meant by parameter (int initialCapacity) in an ArrayList, I thought it's the number of elements but it didn't work when I did this:
public class MyClass {
private ArrayList<Integer> arr;
public MyClass(int n_elements) {
arr = new ArrayList<Integer>(n_elements);
}
}
推荐答案
它是初始容量,即ArrayList将分配的项目数作为项目的内部存储开始.
It's the initial capacity, i.e. the number of items that ArrayList will allocate to begin with as the internal storage of items.
p>
ArrayList 可以包含任意数量的项目"(只要您有内存),并且在进行大型初始插入时,您可以告诉 ArrayList 分配更大的当它尝试为下一个项目分配更多空间时,不要浪费 CPU 周期.
ArrayList can contain "any number of items" (as long you have the memory for it) and when doing large initial insertions you can tell ArrayList to allocate a larger storage to begin with as to not waste CPU cycles when it tries to allocate more space for the next item.
例子:
ArrayList list = new ArrayList<Integer>(2);
list.add(1); // size() == 1
list.add(2); // size() == 2, list is "filled"
list.add(3); // size() == 3, list is expanded to make room for the third element
这篇关于数组列表中的参数(int初始容量)是什么意思的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:数组列表中的参数(int初始容量)是什么意思
基础教程推荐
- RabbitMQ:消息保持“未确认"; 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- 存储 20 位数字的数据类型 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
