1. Overview
1.概述
Java allows us to create arrays of fixed size or use collection classes to do a similar job.
Java允许我们创建固定大小的数组,或者使用集合类来做类似的工作。
In this tutorial, we’re going to look at the difference between the capacity of an ArrayList and the size of an Array.
在本教程中,我们要看一下ArrayList的容量和数组的大小之间的区别。
We’ll also look at examples of when we should initialize ArrayList with a capacity and the benefits and disadvantages in terms of memory usage.
我们还将看一下什么时候应该用容量初始化ArrayList的例子,以及在内存使用方面的好处和坏处。
2. Example
2.例子
To understand the differences, let’s first try both options.
为了了解两者的区别,让我们首先尝试一下这两个选项。
2.1. Size of an Array
2.1.阵列的大小
In java, it’s mandatory to specify the size of an array while creating a new instance of it:
在java中,在创建一个数组的新实例时,必须指定数组的大小。
Integer[] array = new Integer[100];
System.out.println("Size of an array:" + array.length);
Here, we created an Integer array of size 100, which resulted in the below output
在这里,我们创建了一个大小为100的Integer数组,其结果如下
Size of an array:100
2.2. Capacity of an ArrayList
2.2.ArrayList的容量
Now, let’s create an ArrayList with an initial capacity of 100:
现在,让我们创建一个初始容量为100的ArrayList /em>。
List<Integer> list = new ArrayList<>(100);
System.out.println("Size of the list is :" + list.size());
Size of the list is :0
As no elements have been added yet, the size is zero.
由于还没有添加任何元素,所以大小为零。
Now, let’s add an element to the list and check the size of it:
现在,让我们在列表中添加一个元素,并检查它的大小。
list.add(10);
System.out.println("Size of the list is :" + list.size());
Size of the list is :1
3. Size in Arrays vs. ArrayList
3.数组中的大小与ArrayList的对比
Below are some major differences between the size of an array and the capacity of an ArrayList.
下面是数组的大小和ArrayList的容量之间的一些主要区别。
3.1. Modification of Size
3.1.尺寸的修改
Arrays are fixed size. Once we initialize the array with some int value as its size, it can’t change. The size and capacity are equal to each other too.
数组是固定大小的。一旦我们用某个int 值作为数组的大小进行初始化,它就不能改变。大小和容量也是彼此相等的。
ArrayList‘s size and capacity are not fixed. The logical size of the list changes based on the insertion and removal of elements in it. This is managed separately from its physical storage size. Also when the threshold of ArrayList capacity is reached, it increases its capacity to make room for more elements.
ArrayList的大小和容量不是固定的。列表的逻辑大小根据其中元素的插入和移除而变化。这与它的物理存储大小是分开管理的。另外,当达到ArrayListcapacity的阈值时,它将增加其容量以腾出空间容纳更多的元素。
3.2. Memory Allocation
3.2.内存分配
Array memory is allocated on creation. When we initialize an array, it allocates the memory according to the size and type of an array. It initializes all the elements with a null value for reference types and the default value for primitive types.
数组内存是在创建时分配的。当我们初始化一个数组时,它根据数组的大小和类型来分配内存。对于引用类型,它用null值初始化所有元素,对于原始类型,它用默认值初始化。
ArrayList changes memory allocation as it grows. When we specify the capacity while initializing the ArrayList, it allocates enough memory to store objects up to that capacity. The logical size remains 0. When it is time to expand the capacity, a new, larger array is created, and the values are copied to it.
ArrayList随着它的增长而改变内存分配。当我们在初始化ArrayList时指定了容量,它将分配足够的内存来存储该容量的对象。当需要扩大容量时,一个新的、更大的数组被创建,并且值被复制到它。
We should note that there’s a special singleton 0-sized array for empty ArrayList objects, making them very cheap to create. It’s also worth noting that ArrayList internally uses an array of Object references.
我们应该注意到,对于空的ArrayList对象有一个特殊的单子0大小的数组,使得它们的创建非常便宜。同样值得注意的是,ArrayList内部使用一个Object引用数组。
4. When to Initialize ArrayList with Capacity
4.何时用容量初始化ArrayList?
We may expect to initialize the capacity of an ArrayList when we know its required size before we create it, but it’s not usually necessary. However, there are a few reasons why this may be the best option.
当我们在创建一个ArrayList之前知道它所需的大小时,我们可能期望初始化它的容量,但这通常不是必要的。然而,有几个原因说明这可能是最好的选择。
4.1. Building a Large ArrayList
4.1.构建一个大的ArrayList
It is good to initialize a list with an initial capacity when we know that it will get large. This prevents some costly grow operations as we add elements.
当我们知道列表会变大时,用一个初始容量来初始化它是很好的。这可以防止我们在添加元素时进行一些昂贵的增长操作。
Similarly, if the list is very large, the automatic grow operations may allocate more memory than necessary for the exact maximum size. This is because the amount to grow each time is calculated as a proportion of the size so far. So, with large lists, this could result in a waste of memory.
同样地,如果列表非常大,自动增长操作可能会分配比准确的最大尺寸所需的更多的内存。这是因为每次要增长的数量是按迄今为止的大小比例计算的。所以,对于大的列表,这可能会导致内存的浪费。
4.2. Building Small Multiple ArrayLists
4.2.构建小型多重ArrayLists
If we have a lot of small collections, then the automatic capacity of an ArrayList may provide a large percentage of wasted memory. Let’s say that ArrayList prefers a size of 10 with smaller numbers of elements, but we are only storing 2 or 3. That means 70% wasted memory, which might matter if we have a huge number of these lists.
如果我们有很多小集合,那么ArrayList的自动容量可能会提供很大比例的浪费的内存。假设ArrayList在元素数量较少的情况下,倾向于10的大小,但我们只存储2或3。这意味着70%的内存被浪费了,如果我们有大量的这些列表,这可能会很重要。
Setting the capacity upfront can avoid this situation.
预先设定容量可以避免这种情况。
5. Avoiding Waste
5.避免浪费
We should note that ArrayList is a good solution for a flexible-sized container of objects that is to support random access. It consumes slightly more memory than an array but provides a richer set of operations.
我们应该注意到,ArrayList是一个支持随机访问的灵活大小的对象容器的良好解决方案。它比数组消耗的内存略多,但提供了一套更丰富的操作。
In some use cases, especially around large collections of primitive values, the standard array may be faster and use less memory.
在一些用例中,特别是围绕着大量的原始值集合,标准数组可能会更快,并且使用更少的内存。
Similarly, for storing a variable number of elements that do not need to be accessed by index, LinkedList can be more performant. It does not come with any overhead of memory management.
同样地,对于存储不需要通过索引访问的可变数量的元素,LinkedList可以有更高的性能。它不附带任何内存管理的开销。
6. Summary
6.归纳总结
In this short article, we saw the difference between the capacity of the ArrayList and the size of an array. We also looked at when we should initialize the ArrayList with capacity and its benefits with regards to memory usage and performance.
在这篇短文中,我们看到了ArrayList的容量和数组的大小之间的区别。我们还研究了何时应该初始化ArrayList的容量,以及它在内存使用和性能方面的好处。
As always, the example code is available over on GitHub.
像往常一样,示例代码可在GitHub上获得。