数组列表的容量和数组大小的区别 [英] Distinction between the capacity of an array list and the size of an array

查看:40
本文介绍了数组列表的容量和数组大小的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了我所著的 Core Java 中的以下片段.

I read the below snippet in Core Java I book.

将数组列表分配为new ArrayList <'Employee>(100)//容量为 100

与分配新数组不同new Employee[100]//大小为 100

数组列表的容量和数组的大小之间有一个重要的区别大批.如果分配一个有 100 个条目的数组,则该数组有 100 个槽,可供使用.一个容量为 100 个元素的数组列表有可能容纳 100 个元素(并且,事实上,超过 100 个,以额外的重新分配为代价);但一开始,即使在初始构造之后,数组列表根本不包含任何元素.

There is an important distinction between the capacity of an array list and the size of an array. If you allocate an array with 100 entries, then the array has 100 slots, ready for use. An array list with a capacity of 100 elements has the potential of holding 100 elements (and, in fact, more than 100, at the cost of additional reallocations); but at the beginning, even after its initial construction, an array list holds no elements at all.

当我看到源代码数组列表时,构造函数创建了一个给定容量的对象数组,该数组准备保存给定容量的元素(以下是代码片段).

When I saw the source code array list, the constructor creates of an Object array of given capacity which is ready to hold elements of given capacity (below is the code snippet).

public ArrayList(int initialCapacity) {
     super();
     if (initialCapacity < 0)
         throw new IllegalArgumentException("Illegal Capacity: "+
                                            initialCapacity);
     this.elementData = new Object[initialCapacity];
 }

我无法弄清楚作者在上述文本中提到的实际差异.

I am not able to figure out the actual difference what the author has mentioned in above text.

推荐答案

如果您使用 arr = new Employee[100] 分配一个新数组,则该数组的大小 (arr.length) 将是 100.它有 100 个元素.所有元素最初都是空的(因为这是一个对象引用数组),但仍然有 100 个元素.

If you allocate a new array with arr = new Employee[100], the size of that array (arr.length) is going to be 100. It has 100 elements. All the elements are initially null (as this is an array of object references), but still, there are 100 elements.

如果您执行诸如 list = new ArrayList (100) 之类的操作,并尝试检查 list.size(),您将得到 0.列表中没有元素.

If you do something like list = new ArrayList <Employee>(100), and try to check list.size(), you'll get 0. There are no elements in the list.

在内部,ArrayList 确实在需要扩展其容量之前分配了足够的位置来放置 100 个项目,但这是一个内部实现细节,并且列表向您显示其内容为不存储的物品".只有当你真的执行 list.add(something) 时,你才会在列表中看到项目.

Internally, it's true that the ArrayList allocates enough place to put 100 items before it needs to extend its capacity, but that's an internal implementation detail, and the list presents its content to you as "no items stored". Only if you actually do list.add(something), you'll have items in the list.

因此,尽管列表预先分配了存储空间,但它与程序通信的 API 会告诉您其中没有任何项目.您无法使用其内部数组中的空项目 - 您无法检索或更改它们.

So although the list allocates storage in advance, the API with which it communicates with the program tells you there are no items in it. The null items in its internal array are not available to you - you cannot retrieve them or change them.

这篇关于数组列表的容量和数组大小的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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