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

发布于 2024-09-24 14:28:44 字数 65 浏览 2 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

说不完的你爱 2024-10-01 14:28:44

这取决于 List 实现。由于您使用 int 索引数组,因此 ArrayList 不能容纳超过 Integer.MAX_VALUE 元素。不过,LinkedList 并不以同样的方式受到限制,并且可以包含任意数量的元素。

编辑:我意识到这听起来像是对LinkedList的认可。但请注意,尽管它们没有理论容量限制,链接列表通常是一个较差的选择基于数组的数据结构,因为每个元素的内存使用量较高(这降低了列表的实际容量限制)和较差的缓存局部性(因为节点分布在整个 RAM 上)。如果您的项目数量多于数组所能容纳的数量,那么这绝对不是您想要的数据结构。

It depends on the List implementation. Since you index arrays with ints, an ArrayList can't hold more than Integer.MAX_VALUE elements. A LinkedList 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.

冰火雁神 2024-10-01 14:28:44

这取决于实现,但 List 接口未定义限制。

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

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

因此,没有限制,但在达到 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 an 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

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

爱*していゐ 2024-10-01 14:28:44

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

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

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

返回一个数组,其中按正确顺序(从第一个元素到最后一个元素)包含此列表中的所有元素;
忠实地实现某些方法会遇到困难,例如

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

有些人会认为 size() 的文档...

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

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

数组列表有默认大小吗?

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

ArrayList 的大小限制为 Integer.MAX_VALUE,因为它由普通数组支持。

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

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 of List.toArray however, states that ...

Return an array containing all of 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.

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

Returns the number of elements in this list. If this list contains more than Integer.MAX_VALUE elements, returns 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 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 is Integer.MAX_VALUE since it's backed by an ordinary array.

泪之魂 2024-10-01 14:28:44

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

通常,List 实现可以容纳任意数量的项目(如果您使用索引列表,则可能仅限于 Integer.MAX_VALUELong.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 or Long.MAX_VALUE). As long as you don't run out of memory, the List doesn't become "full" or anything.

吃素的狼 2024-10-01 14:28:44

只要您的可用内存允许,就可以。除了堆之外,没有大小限制。

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

泪之魂 2024-10-01 14:28:44

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

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

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

ArrayList(已标记)由数组支持,并且受到数组大小的限制 - 即 Integer.MAX_VALUE

The interface however defines the size() method, which returns an 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

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

黯然#的苍凉 2024-10-01 14:28:44

请参阅下面的 arraylist 代码,创建时默认它是 10
列表 l = new ArrayList();

   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);
          }

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

   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);
          }
蔚蓝源自深海 2024-10-01 14:28:44

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文