最大限度。Java中列表的长度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4498805/
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
max. length of List in Java
提问by kandarp
What is the maximum length of List in java? I mean that how many maximum elements can store in list?
java中List的最大长度是多少?我的意思是列表中可以存储多少个最大元素?
采纳答案by Jigar Joshi
Integer.MAX_VALUE
or heap which ever is low will be the limit
或低的堆将是限制
回答by musiKk
This depends on the specific list implementation. Most people think it's Integer.MAX_VALUE
but it isn't limited to that. In fact the documentation of the size()
method merely states:
这取决于特定的列表实现。大多数人认为它是,Integer.MAX_VALUE
但不仅限于此。事实上,该size()
方法的文档仅说明:
If this list contains more than
Integer.MAX_VALUE
elements, returnsInteger.MAX_VALUE
.
如果此列表包含多个
Integer.MAX_VALUE
元素,则返回Integer.MAX_VALUE
。
So it might as well contain more elements.
所以它也可能包含更多的元素。
回答by dogbane
It's very likely that you will run out of heap space well before you get anywhere close to Integer.MAX_VALUE
number of elements, but let's see what would happen if we had infinite memory and tried to add more than Integer.MAX_VALUE
elements to a List
:
在接近Integer.MAX_VALUE
元素数量之前,很可能会用完堆空间,但是让我们看看如果我们有无限内存并尝试向 a 添加多个Integer.MAX_VALUE
元素会发生什么List
:
1) ArrayList:
1) 数组列表:
An ArrayList will try to increase its capacity:
ArrayList 将尝试增加其容量:
int newCapacity = (oldCapacity * 3)/2 + 1;
This calculation will overflow and newCapacity
will actually be less than Integer.MAX_VALUE
. The ArrayList
will then try to copy the elements in the original array to a smaller array and will hence lose elements.
此计算将溢出并且newCapacity
实际上将小于Integer.MAX_VALUE
。然后ArrayList
将尝试将原始数组中的元素复制到较小的数组中,因此会丢失元素。
2) LinkedList:
2)链表:
A LinkedList
works a bit better once you cross the Integer.MAX_VALUE
limit. It will continue to hold elements, however the size
attribute will suffer from integer overflow and will affect other operations that make use of it.
LinkedList
一旦Integer.MAX_VALUE
超过限制,A 的效果会更好。它将继续保存元素,但是该size
属性将遭受整数溢出并影响使用它的其他操作。