简体   繁体   English

ArrayList 容量限制限制

[英]ArrayList Capacity Limit restriction

I have searched for ArrayList capacity questions but could not find a complete answer.我搜索了 ArrayList 容量问题,但找不到完整的答案。 so asking it again here.所以在这里再次询问。

I understand that size the number of elements we add in ArrayList and capacity is how much data we can put in that list with the default value to be 10.我知道我们在 ArrayList 中添加的元素数量和容量是我们可以在该列表中放入多少数据,默认值为 10。

So the question here is while declaring if give the capacity to be like this所以这里的问题是在声明是否赋予这样的能力时

List<String> list = new ArrayList<>(1);

Then also I can keep on adding the elements up to 10 or 20. So is this capacity declaration useful only to internal reallocation which happens when it reaches the capacity?然后我也可以继续添加最多 10 或 20 个元素。那么这个容量声明是否仅对达到容量时发生的内部重新分配有用?

Or by giving the capacity limit can we restrict only to that point of add elements?或者通过给出容量限制,我们可以只限制添加元素的那个点吗?

The initial capacity doesn't determine how many elements you can add to the ArrayList .初始容量并不决定您可以向ArrayList添加多少元素。 The capacity is automatically increased when necessary as elements are added.随着元素的添加,容量会在必要时自动增加。

The motivation to specify an initial capacity is performance.指定初始容量的动机是性能。 If you know that your ArrayList will contain a million elements, it's more efficient to create the ArrayList with an initial capacity of 1000000, since that will save the need to resize the ArrayList capacity multiple times as elements are added.如果你知道你ArrayList将包含上百万元素,它更有效地创建ArrayList有100万的初始容量,因为这将节省需要调整ArrayList多次容量元素的添加。

ArrayList are backed by an array which max size is Integer.MAX_VALUE - 8. ArrayList 由最大大小为 Integer.MAX_VALUE - 8 的数组支持。

This is referred in these answers:这些答案中提到了这一点:

ArrayLists and other collections will grow as their capacity is used up. ArrayLists 和其他集合会随着它们的容量用完而增长。 However how they do this depends on what data-structures are used 'behind the scenes'然而,他们如何做到这一点取决于“幕后”使用的数据结构

An array list is backed by a Java Array.数组列表由 Java 数组支持。 When that array is close to being filled a new Array is created, normally with twice the capacity, then all the elements are copied from the old to the new array, and the ArrayList then points at the new array.当该数组接近填充时,会创建一个新数组,通常具有两倍的容量,然后将所有元素从旧数组复制到新数组,然后 ArrayList 指向新数组。

Since the arrays double each time, adding an element to an array is still a constant time operation or "O(1)" in bigO notation.由于数组每次都加倍,因此向数组添加元素仍然是常量时间操作或 bigO 表示法中的“O(1)”。 It is done in "amortized time"它是在“摊销时间”内完成的

However if you know your ArrayList is likely to contain n elements, you can declare it with an initial size of n .但是,如果您知道 ArrayList 可能包含n元素,则可以使用n的初始大小声明它。 This makes the initial Array big enough to hold the elements you expect it to without having to do any "resizing".这使得初始 Array 足够大以容纳您期望的元素,而无需进行任何“调整大小”。 (Creating bigger arrays and copying elements over). (创建更大的数组并复制元素)。

However it's not an issue if you underestimate n as the ArrayList will still grow to handle the extra elements.但是,如果您低估n也不是问题,因为 ArrayList 仍然会增长以处理额外的元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM