Java List最多可以容纳多少数据?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3767979/
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 04:39:16  来源:igfitidea点击:

How much data can a List can hold at the maximum?

javaarraylistsize

提问by Paul

How much data can be added in java.util.List in Java at the maximum?

Java中java.util.List最多可以添加多少数据?

Is there any default size of an ArrayList?

ArrayList 是否有任何默认大小?

采纳答案by gustafc

It depends on the Listimplementation. Since you index arrays with ints, an ArrayListcan't hold more than Integer.MAX_VALUEelements. A LinkedListisn't limited in the same way, though, and can contain any amount of elements.

这取决于List实现。由于您使用ints索引数组,因此 anArrayList不能容纳多个Integer.MAX_VALUE元素。但是,ALinkedList不受限制,可以包含任意数量的元素。

回答by duffymo

As much as your available memory will allow. There's no size limit except for the heap.

尽可能多的可用内存。除了堆之外没有大小限制。

回答by Bozho

It would depend on the implementation, but the limit is not defined by the Listinterface.

这将取决于实现,但限制不是由List接口定义的。

The interface however defines the size()method, which returns an int.

然而,接口定义了size()方法,该方法返回一个int.

Returns the number of elements in this list. If this list contains more than Integer.MAX_VALUEelements, returns Integer.MAX_VALUE.

返回此列表中的元素数。如果此列表包含多个Integer.MAX_VALUE元素,则返回Integer.MAX_VALUE

So, no limit, but after you reach Integer.MAX_VALUE, the behaviour of the list changes a bit

所以,没有限制,但是在你到达之后Integer.MAX_VALUE,列表的行为会发生一些变化

ArrayList(which is tagged) is backed by an array, and is limited to the size of the array - i.e. Integer.MAX_VALUE

ArrayList(被标记)由一个数组支持,并且受限于数组的大小 - 即 Integer.MAX_VALUE

回答by Gerco Dries

java.util.Listis an interface. How much data a list can hold is dependant on the specific implementation of List you choose to use.

java.util.List是一个接口。一个列表可以容纳多少数据取决于您选择使用的 List 的具体实现。

Generally, a List implementation can hold any number of items (If you use an indexed List, it may be limited to Integer.MAX_VALUEor Long.MAX_VALUE). As long as you don't run out of memory, the List doesn't become "full" or anything.

通常,一个 List 实现可以容纳任意数量的项目(如果您使用索引 List,它可能仅限于Integer.MAX_VALUELong.MAX_VALUE)。只要您没有耗尽内存,列表就不会变得“满”或任何东西。

回答by aioobe

How much data can be added in java.util.List in Java at the maximum?

Java中java.util.List最多可以添加多少数据?

This is very similar to Theoretical limit for number of keys (objects) that can be stored in a HashMap?

这与可以存储在 HashMap 中的键(对象)数量的理论限制非常相似

The documentation of java.util.Listdoes not explicitly documented any limit on the maximum number of elements. The documentation of List.toArrayhowever, states that ...

的文档java.util.List没有明确记录对最大元素数量的任何限制。List.toArray然而,文档指出......

Return an array containing allof the elements in this list in proper sequence (from first to last element); would have trouble implementing certain methods faithfully, such as

以适当的顺序(从第一个元素到最后一个元素)返回一个包含此列表中所有元素的数组;将难以忠实地实施某些方法,例如

... 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.

...所以严格来说,如果列表有超过 2 31-1 = 2147483647 个元素,则不可能忠实地实现此方法,因为这是最大的可能数组。

Some will argue that the documentation of size()...

有些人会争辩说size()...的文档

Returns the number of elements in this list. If this list contains more than Integer.MAX_VALUEelements, returns Integer.MAX_VALUE.

返回此列表中的元素数。如果此列表包含多个Integer.MAX_VALUE元素,则返回Integer.MAX_VALUE

...indicates that there is no upper limit, but this view leads to numerous inconsistencies. See this bug report.

...表示没有上限,但这种观点导致了许多不一致。请参阅此错误报告

Is there any default size an array list?

数组列表是否有任何默认大小?

If you're referring to ArrayListthen I'd say that the default size is 0. The default capacityhowever (the number of elements you can insert, without forcing the list to reallocate memory) is 10. See the documentation of the default constructor.

如果您指的是,ArrayList那么我会说默认大小为 0。然而,默认容量(您可以插入的元素数,而不强制列表重新分配内存)为 10。请参阅默认构造函数的文档。

The size limit of ArrayListis Integer.MAX_VALUEsince it's backed by an ordinary array.

的大小限制ArrayListInteger.MAX_VALUE因为它由一个普通数组支持。

回答by Dead Programmer

see the code below of arraylist default it is 10 when u create List l = new ArrayList();

请参阅下面的代码,当您创建 List l = new ArrayList() 时,arraylist 默认为 10;

   public class ArrayList<E> extends AbstractList<E> implements List<E>,
           Cloneable, Serializable, RandomAccess {

          private static final long serialVersionUID = 8683452581122892189L;

          private transient int firstIndex;

          private transient int lastIndex;

          private transient E[] array;

          /**
           * Constructs a new instance of {@code ArrayList} with ten capacity.
           */
          public ArrayList() {
              this(10);
          }

回答by Mohammad

The interface however defines the size() method, which returns an int.

然而,该接口定义了 size() 方法,该方法返回一个 int。

Returns the number of elements in this list. If this list contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.

So, no limit, but after you reach Integer.MAX_VALUE, the behaviour of the list changes a bit

所以,没有限制,但是当你达到 Integer.MAX_VALUE 后,列表的行为会发生一些变化

ArrayList (which is tagged) is backed by an array, and is limited to the size of the array - i.e. Integer.MAX_VALUE

ArrayList(带标签)由一个数组支持,并且受限于数组的大小 - 即 Integer.MAX_VALUE

回答by Oleksy Ostanin

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.

对 java 数组中的项目进行编号应该从零开始。这是我认为我们可以访问 Integer.MAX_VALUE+1 个项目。