1. Overview
1.概述
In this tutorial, we’ll take a look at the different ways in which we can extend a Java array.
在本教程中,我们将看看我们可以通过哪些不同的方式来扩展Java 数组。
Since arrays are a contiguous block of memory, the answer may not be readily apparent, but let’s unpack that now.
由于数组是一个连续的内存块,答案可能不是很明显,但我们现在来解开这个问题。
2. Using Arrays.copyOf
2.使用Arrays.copyOf
First, let’s look at Arrays.copyOf. We’ll copy the array and add a new element to the copy:
首先,让我们看一下Arrays.copyOf。我们将复制数组并在副本中添加一个新元素。
public Integer[] addElementUsingArraysCopyOf(Integer[] srcArray, int elementToAdd) {
Integer[] destArray = Arrays.copyOf(srcArray, srcArray.length + 1);
destArray[destArray.length - 1] = elementToAdd;
return destArray;
}
The way Arrays.copyOf works is that it takes the srcArray and copies the number of elements specified in the length argument to a new array which it internally creates. The size of the new array is the argument that we provide.
Arrays.copyOf的工作方式是,它接收srcArray并将length参数中指定的元素数量复制到它内部创建的一个新数组。新数组的大小是我们提供的参数。
One thing to notice is that when the length argument is greater than the size of the source array, Arrays.copyOf will fill the extra elements in the destination array with null.
有一点需要注意的是,当长度参数大于源数组的大小时,Arrays.copyOf将用null填充目标数组中的额外元素。
Depending on the datatype, the behavior of the filling will be different. For example, if we use primitive data types in place of Integer then the extra elements are filled with the zeros. In the case of char, Arrays.copyOf will fill extra elements with null and in case of boolean, with false.
根据数据类型的不同,填充的行为也会不同。例如,如果我们使用原始数据类型来代替Integer,那么额外的元素将被填充为零。如果是char,Arrays.copyOf将用null填充额外的元素,如果是boolean,用false。
3. Using ArrayList
3.使用ArrayList
The next way we’ll look at is using ArrayList.
我们要看的下一个方法是使用ArrayList.。
We’ll first convert the array to an ArrayList and then add the element. Then we’ll convert the ArrayList back to an array:
我们首先将数组转换为ArrayList,然后添加元素。然后,我们将把ArrayList转换回数组。
public Integer[] addElementUsingArrayList(Integer[] srcArray, int elementToAdd) {
Integer[] destArray = new Integer[srcArray.length + 1];
ArrayList<Integer> arrayList = new ArrayList<>(Arrays.asList(srcArray));
arrayList.add(elementToAdd);
return arrayList.toArray(destArray);
}
Note that we’ve passed the srcArray by converting it to a Collection. The srcArray will populate the underlying array in the ArrayList.
请注意,我们已经通过将srcArray转换为Collection>来传递。srcArray将填充在ArrayList中的基础数组。
Also, another thing to note is that we’ve passed the destination array as an argument to toArray. This method will copy the underlying array to the destArray.
另外,需要注意的是,我们把目标数组作为参数传给toArray。这个方法将复制底层数组到destArray。
4. Using System.arraycopy
4.使用System.arraycopy
Finally, we’ll take a look at System.arraycopy, which quite similar to Arrays.copyOf:
最后,我们来看看System.arraycopy,它与Arrays.copyOf相当相似。
public Integer[] addElementUsingSystemArrayCopy(Integer[] srcArray, int elementToAdd) {
Integer[] destArray = new Integer[srcArray.length + 1];
System.arraycopy(srcArray, 0, destArray, 0, srcArray.length);
destArray[destArray.length - 1] = elementToAdd;
return destArray;
}
One interesting fact is that Arrays.copyOf internally uses this method.
一个有趣的事实是,Arrays.copyOf在内部使用了这个方法。
Here we can notice that we copy the elements from the srcArray to destArray and then add the new element to the destArray.
这里我们可以注意到,我们从srcArray复制元素到destArray,然后将新元素添加到destArray。
5. Performance
5.业绩
One thing common in all the solutions is that we have to create a new array one way or another. The reason for it lies in how arrays are allocated in memory. An array holds a contiguous block of memory for super-fast lookup, which is why we cannot simply resize it.
所有的解决方案都有一个共同点,那就是我们必须以这种或那种方式创建一个新的数组。其原因在于数组是如何在内存中分配的。一个数组持有一个连续的内存块用于超快速的查找,这就是为什么我们不能简单地调整它的大小。
This, of course, has a performance impact, especially for large arrays. This is why ArrayList over-allocates, effectively reducing the number of times the JVM needs to reallocate memory.
当然,这对性能是有影响的,特别是对于大型数组。这就是为什么ArrayList会过度分配,有效减少JVM需要重新分配内存的次数。
But, if we are doing a lot of inserts, an array might not be the right data structure, and we should consider a LinkedList.
但是,如果我们要做大量的插入,数组可能不是正确的数据结构,我们应该考虑使用LinkedList。
6. Conclusion
6.结论
In this article, we have explored the different ways of adding elements to the end of an array.
在这篇文章中,我们已经探讨了向数组末端添加元素的不同方法。
And, as always, the entire code is available over on GitHub.
而且,像往常一样,整个代码都可以在GitHub上找到。