List最多可以容纳多少数据?
Java中java.util.List中最多可以添加多少数据?
ArrayList 有默认大小吗?
How much data can be added in java.util.List in Java at the maximum?
Is there any default size of an ArrayList?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
这取决于
List
实现。由于您使用int
索引数组,因此ArrayList
不能容纳超过Integer.MAX_VALUE
元素。不过,LinkedList
并不以同样的方式受到限制,并且可以包含任意数量的元素。编辑:我意识到这听起来像是对
LinkedList
的认可。但请注意,尽管它们没有理论容量限制,链接列表通常是一个较差的选择基于数组的数据结构,因为每个元素的内存使用量较高(这降低了列表的实际容量限制)和较差的缓存局部性(因为节点分布在整个 RAM 上)。如果您的项目数量多于数组所能容纳的数量,那么这绝对不是您想要的数据结构。It depends on the
List
implementation. Since you index arrays withint
s, anArrayList
can't hold more thanInteger.MAX_VALUE
elements. ALinkedList
isn't limited in the same way, though, and can contain any amount of elements.Edit: I realize this sounds like an endorsement of
LinkedList
. But please note that although they don't have a theoretical capacity limit, linked lists are usually an inferior choice to array-based data structures because of higher memory use per element (which lowers the list's actual capacity limit) and poor cache locality (because nodes are spread all over RAM). It's definitely not the data structure you want if you have more items than can be crammed into an array.这取决于实现,但
List
接口未定义限制。然而,该接口定义了
size()
方法,该方法返回一个int
。因此,没有限制,但在达到
Integer.MAX_VALUE
后,列表的行为会发生一点ArrayList
(已标记)的支持由数组组成,并且受限于数组的大小 - 即Integer.MAX_VALUE
It would depend on the implementation, but the limit is not defined by the
List
interface.The interface however defines the
size()
method, which returns anint
.So, no limit, but after you reach
Integer.MAX_VALUE
, the behaviour of the list changes a bitArrayList
(which is tagged) is backed by an array, and is limited to the size of the array - i.e.Integer.MAX_VALUE
这与理论限制非常相似可以存储在 HashMap 中的键(对象)数量?
java.util.List 的文档没有明确记录对最大元素数量的任何限制。 < 的文档code>List.toArray 然而,声明...
......所以严格来说,如果列表超过 231-1 = 2147483647 个元素,则不可能忠实地实现此方法,因为这是最大可能的数组。
有些人会认为
size()
的文档......表明没有上限,但这种观点会导致许多不一致。请参阅此错误报告。
如果您指的是
ArrayList
,那么我会说默认大小为 0。默认容量 em> 但是(可以插入的元素数量,无需强制列表重新分配内存)为 10。请参阅 默认构造函数。ArrayList
的大小限制为Integer.MAX_VALUE
,因为它由普通数组支持。This is very similar to Theoretical limit for number of keys (objects) that can be stored in a HashMap?
The documentation of
java.util.List
does not explicitly documented any limit on the maximum number of elements. The documentation ofList.toArray
however, states that ...... so strictly speaking it would not be possible to faithfully implement this method if the list had more than 231-1 = 2147483647 elements since that is the largest possible array.
Some will argue that the documentation of
size()
......indicates that there is no upper limit, but this view leads to numerous inconsistencies. See this bug report.
If you're referring to
ArrayList
then I'd say that the default size is 0. The default capacity however (the number of elements you can insert, without forcing the list to reallocate memory) is 10. See the documentation of the default constructor.The size limit of
ArrayList
isInteger.MAX_VALUE
since it's backed by an ordinary array.java.util.List
是一个接口。列表可以容纳多少数据取决于您选择使用的 List 的具体实现。通常,List 实现可以容纳任意数量的项目(如果您使用索引列表,则可能仅限于
Integer.MAX_VALUE
或Long.MAX_VALUE
)。只要你没有耗尽内存,列表就不会变得“满”或任何其他情况。java.util.List
is an interface. How much data a list can hold is dependant on the specific implementation of List you choose to use.Generally, a List implementation can hold any number of items (If you use an indexed List, it may be limited to
Integer.MAX_VALUE
orLong.MAX_VALUE
). As long as you don't run out of memory, the List doesn't become "full" or anything.只要您的可用内存允许,就可以。除了堆之外,没有大小限制。
As much as your available memory will allow. There's no size limit except for the heap.
然而,该接口定义了 size() 方法,该方法返回一个 int。
因此,没有限制,但是在达到 Integer.MAX_VALUE 后,列表的行为会发生一些变化
ArrayList(已标记)由数组支持,并且受到数组大小的限制 - 即 Integer.MAX_VALUE
The interface however defines the size() method, which returns an int.
So, no limit, but after you reach Integer.MAX_VALUE, the behaviour of the list changes a bit
ArrayList (which is tagged) is backed by an array, and is limited to the size of the array - i.e. Integer.MAX_VALUE
请参阅下面的 arraylist 代码,创建时默认它是 10
列表 l = new ArrayList();
see the code below of arraylist default it is 10 when u create
List l = new ArrayList();
java 数组中的项目编号应从零开始。我认为我们可以访问 Integer.MAX_VALUE+1 项目。
Numbering an items in the java array should start from zero. This was i think we can have access to Integer.MAX_VALUE+1 an items.