最大限度。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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 18:03:59  来源:igfitidea点击:

max. length of List in Java

javalist

提问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_VALUEbut 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_VALUEelements, returns Integer.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_VALUEnumber of elements, but let's see what would happen if we had infinite memory and tried to add more than Integer.MAX_VALUEelements 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 newCapacitywill actually be less than Integer.MAX_VALUE. The ArrayListwill 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 LinkedListworks a bit better once you cross the Integer.MAX_VALUElimit. It will continue to hold elements, however the sizeattribute will suffer from integer overflow and will affect other operations that make use of it.

LinkedList一旦Integer.MAX_VALUE超过限制,A 的效果会更好。它将继续保存元素,但是该size属性将遭受整数溢出并影响使用它的其他操作。