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

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

问题描述

我读了Java核心我的书下面的代码片段。

I read the below snippet in Core Java I book.

分配一个数组列表,
  新的ArrayList<'员工>(100)//容量为100

是不一样的分配一个新的数组作为
  新员工[100] //大小为100

is not the same as allocating a new array as new Employee[100] // size is 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.

当我看到源$ C ​​$ C数组列表,构造的给定容量对象数组这是准备持有给出电容元件创建(下面是code段)。

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 =新员工[100] ,即数组的大小( arr.length )将是100,有100个元素。所有的元素都是最初为null(因为这是对象引用数组),不过,也有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.

如果你做的东西像名单=新的ArrayList&LT;员工&GT;(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个项目之前,它需要扩大产能,但是这是一个内部实现细节,而列表presents其内容为你无物品存放。只有当你真正做 list.add(东西),你就会有列表中的项目。

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天全站免登陆