Java 数组列表中的参数(int 初始容量)是什么意思
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4172480/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's meant by parameter (int initial capacity) in an arraylist
提问by Ismail Marmoush
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:
(int initialCapacity)
an 中的参数是什么意思ArrayList
,我认为它是元素的数量,但是当我这样做时它不起作用:
public class MyClass {
private ArrayList<Integer> arr;
public MyClass(int n_elements) {
arr = new ArrayList<Integer>(n_elements);
}
}
采纳答案by Patrick
It's the initial capacity, i.e. the number of items that ArrayList
will allocate to begin with as the internal storage of items.
它是初始容量,即ArrayList
作为项目内部存储开始分配的项目数量。
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
可以包含“任意数量的项目”(只要您有内存),并且在进行大量初始插入时,您可以告诉ArrayList
开始分配更大的存储空间,以免在尝试分配更多空间时浪费 CPU 周期下一项。
Example:
例子:
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
回答by Carl
Practically speaking, it's how many elements you can add to the ArrayList
before it resizes in the background, which can save you some cycles if used correctly.
实际上,它是ArrayList
在后台调整大小之前您可以添加多少元素,如果使用得当,可以为您节省一些周期。
回答by dacwe
Capacity is the size of the internal storage of the objects. The internal storage is always greater than or equal to the size() of the list (so that it can contain all elements).
容量是对象内部存储的大小。内部存储总是大于或等于列表的 size() (以便它可以包含所有元素)。
public class Main {
public static void main(String[] args) throws Exception {
ArrayList<Integer> arr = new ArrayList<>();
System.out.println("initial size = " + arr.size()); // 0
System.out.println("initial capacity = " + getCapacity(arr));
for (int i = 0; i < 11; i++)
arr.add(i);
System.out.println("size = " + arr.size()); // 11
System.out.println("capacity = " + getCapacity(arr));
}
static int getCapacity(ArrayList<?> l) throws Exception {
Field dataField = ArrayList.class.getDeclaredField("elementData");
dataField.setAccessible(true);
return ((Object[]) dataField.get(l)).length;
}
}
Running this gives:
运行这个给出:
initial size = 0
initial capacity = 10
size = 11
capacity = 16
回答by Legato
Under the hood, ArrayList
is essentially a dynamic array. Every time you instantiate using new Arraylist<>()
what's happening is that an array is created to hold the values you want to store whose default capacity, not to be confused with size, is 10.
在引擎盖下,ArrayList
本质上是一个动态数组。每次使用new Arraylist<>()
发生的情况进行实例化时,都会创建一个数组来保存要存储的值,其默认容量(不要与 size 混淆)为 10。
Every time you add a value that would increase the size beyond capacity a new array is created whose capacity is one more than 150% the previous capacity with the contents of the previous array copied within.
每次添加一个会增加超出容量大小的值时,都会创建一个新数组,其容量是前一个容量的 150% 以上,其中复制了前一个数组的内容。
If you have a general idea what size the resulting list will be, or are certain but desire the flexibility afforded from using arraylists over arrays you can set the capacity to prevent this repetitive process of creating new arrays, copying the contents of the old array in the new one, and getting rid of the old one -- which will otherwise increase in occurrences proportional to the size of the list.
如果您大致了解结果列表的大小,或者确定但希望通过在数组上使用数组列表提供灵活性,您可以设置容量以防止创建新数组的重复过程,复制旧数组的内容新的,并摆脱旧的——否则,出现的次数将与列表的大小成正比。