ArrayList的:如何规模增加? [英] ArrayList: how does the size increase?

查看:226
本文介绍了ArrayList的:如何规模增加?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Java的一个基本问题的ArrayList

I have a basic question on Java ArrayList.

的ArrayList 声明,并使用默认的构造函数初始化,10元的内存空间中创建。现在,当我添加一个11元,会发生什么?将新的内存空间,可以与20个(或以上)的元素创造能力(这需要从第一个内存位置移动到新位置复制的元素)或一些其他的东西?

When ArrayList is declared and initialized using the default constructor, memory space for 10 elements is created. Now, when I add an 11th element, what happens? Will new memory space be created with 20 (or more) element capacity (this requires copying elements from 1st memory location to new location) OR some thing else?

我这里检查。但我没有找到答案。

I checked here. But I didn't find an answer.

请分享知识。
谢谢你。

Please share the knowledge. Thanks.

推荐答案

创建一个新的阵列和旧的内容复制过来。这是所有你知道的,在API级别。从的文档(我的重点):

A new array is created and the contents of the old one are copied over. That's all you know at the API level. Quoting from the docs (my emphasis):

每个的ArrayList 实例都有一个容量。容量是用于存储在列表中的元件的阵列的大小。它总是至少与列表大小一样大。随着元素被添加到一个ArrayList,其容量会自动增加。 未指定超越的事实,添加元素会在固定时间摊销费用增长政策的细节。

Each ArrayList instance has a capacity. The capacity is the size of the array used to store the elements in the list. It is always at least as large as the list size. As elements are added to an ArrayList, its capacity grows automatically. The details of the growth policy are not specified beyond the fact that adding an element has constant amortized time cost.

在如何它实际上与具体实施的ArrayList (如Sun的),在他们的情况下,你可以看到在源血淋淋的细节发生条款。不过,当然,依靠具体实施的细节通常不是个好主意......

In terms of how it actually happens with a specific implementation of ArrayList (such as Sun's), in their case you can see the gory details in the source. But of course, relying on the details of a specific implementation isn't usually a good idea...

这篇关于ArrayList的:如何规模增加?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆