1. Overview
1.概述
In this quick tutorial, we will learn about the various ways in which we can remove an element from an array in Java using the Apache Commons Lang library.
在这个快速教程中,我们将学习使用Apache Commons Lang库,在Java中从数组中删除一个元素的各种方法。
2. Maven
2.雯雯
Let’s add the commons-lang3 dependency to our project’s pom.xml file:
让我们把commons-lang3依赖添加到我们项目的pom.xml文件。
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
3. Removing an Element
3.移除一个元素
Before we get started, let’s look at what happens when we remove an element from an array without using the ArrayUtils class from the Apache Commons Lang library.
在我们开始之前,让我们看看当我们不使用Apache Commons Lang库中的ArrayUtils类而从数组中移除一个元素时会发生什么。
Given the array below, let’s remove an element at index 2:
给定下面的数组,让我们删除一个位于索引2的元素。
A simple way of doing this would be to replace the value stored at index 2 with the value stored at index 3 until we reach the end of the array:
一个简单的方法是用存储在索引2的值替换存储在索引3的值,直到我们到达数组的末端。
Notice that by removing the element in the above manner, the size of the array would remain the same and the value stored at the last index would be empty. Since arrays have a fixed memory size allocated during initialization, removing an element does not adjust the size of the array.
请注意,通过上述方式删除元素,数组的大小将保持不变,存储在最后一个索引的值将为空。因为数组在初始化时有一个固定的内存大小分配,移除一个元素并不会调整数组的大小。
Now let’s look at the array representation when removing an element using the remove method from ArrayUtils class from Apache Commons Lang:
现在让我们看看使用Apache Commons Lang的remove 类中的ArrayUtils方法删除一个元素时的数组表示。
As we can see, the array size here is adjusted to 5 after the element is removed. The remove method creates a brand new array and copies all the values except for the value being removed.
我们可以看到,这里的数组大小在元素被移除后被调整为5。remove 方法创建了一个全新的数组,并复制了所有的值,除了被删除的值。
The ArrayUtils class provides two ways of removing an element from an array. Let’s look at these next.
ArrayUtils类提供了两种从数组中移除一个元素的方法。让我们接下来看看这些。
4. Using Index as Input
4.使用索引作为输入
The first way we can remove an element is by its index with ArrayUtils#remove:
我们可以通过ArrayUtils#remove来移除一个元素的第一个方法是通过其索引。
public int[] removeAnElementWithAGivenIndex(int[] array, int index) {
return ArrayUtils.remove(array, index);
}
Another variation is the removeAll method, which we can use to remove multiple elements from an array, given their indices:
另一个变化是removeAll方法,我们可以用它来从一个数组中删除多个元素,给定它们的索引。
public int[] removeAllElementsWithGivenIndices(int[] array, int... indices) {
return ArrayUtils.removeAll(array, indices);
}
5. Using Element as Input
5.使用元素作为输入
Or, let’s say we don’t know the index of what we are removing. In that case, we can provide the element to remove using ArrayUtils#removeElement:
或者说,我们不知道我们要移除的东西的索引。在这种情况下,我们可以使用ArrayUtils#removeElement提供要删除的元素。
public int[] removeFirstOccurrenceOfGivenElement(int[] array, int element) {
return ArrayUtils.removeElement(array, element);
}
Here’s another useful variation of this method ArrayUtils#removeElements, in case there is more than one element that we would like to remove:
下面是这个方法的另一个有用的变体ArrayUtils#removeElements,如果有不止一个元素我们想移除的话。
public int[] removeAllGivenElements(int[] array, int... elements) {
return ArrayUtils.removeElements(array, elements);
}
Sometimes, we would want to remove all occurrences of a given element. We can do so by using ArrayUtils#removeAllOccurences:
有时,我们会想删除一个给定元素的所有出现次数。我们可以通过使用ArrayUtils#removeAllOccurences来实现。
public int[] removeAllOccurrencesOfAGivenElement(int[] array, int element) {
return ArrayUtils.removeAllOccurences(array, element);
}
6. Conclusion
6.结语
In this article, we looked at the various ways of removing an element/elements from an array using the Apache Commons Lang library.
在这篇文章中,我们研究了使用Apache Commons Lang库从数组中移除一个/多个元素的各种方法。
To learn more about the edge cases, please check out the source code for this tutorial and the relevant unit tests available on GitHub.
要了解有关边缘案例的更多信息,请查看本教程的源代码和相关的单元测试可在GitHub上获得。