Removing the First Element of an Array – 移除数组的第一个元素

最后修改: 2018年 8月 19日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this tutorial, we’ll look at how to remove the first element of an array.

在本教程中,我们将研究如何删除数组的第一个元素

In addition, we’ll also see how using data structures from the Java Collections Framework makes things even easier.

此外,我们还将看到如何使用Java集合框架中的数据结构,使事情变得更加简单。

2. Using Arrays.copyOfRange()

2.使用Arrays.copyOfRange()

First of all, removing an element of an array isn’t technically possible in Java. To quote the official docs:

首先,删除数组中的一个元素在技术上是不可能的。引用官方文档

“An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed.”

“一个数组是一个容器对象,它可以容纳固定数量的单一类型的值。数组的长度是在创建数组的时候确定的。创建后,其长度是固定的。”

This means as long as we’re working with an array directly, all we can do is create a new array of smaller size, which then doesn’t contain the first element.

这意味着只要我们直接处理一个数组,我们能做的就是创建一个尺寸更小的新数组,然后不包含第一个元素

Luckily the JDK provides a convenient static helper function we can use, called Arrays.copyOfRange():

幸运的是,JDK提供了一个方便的静态辅助函数,我们可以使用,叫做Arrays.copyOfRange()

String[] stringArray = {"foo", "bar", "baz"};
String[] modifiedArray = Arrays.copyOfRange(stringArray, 1, stringArray.length);

Note that this operation has a cost of O(n) since it will create a new array every time.

注意,该操作的成本为O(n),因为它每次都会创建一个新的数组。

Of course, this is a cumbersome way to remove an element from the array and if you are doing such operations regularly, it might be more sensible to use the Java Collections Framework instead.

当然,这是从数组中删除一个元素的繁琐方法,如果你经常做这样的操作,使用Java集合框架可能更明智。

3. Using a List Implementation

3.使用List实现

In order to keep roughly the same semantics of the data structure (an ordered sequence of elements that are accessible by index), it makes sense to use an implementation of the List interface.

为了保持数据结构的大致相同的语义(可通过索引访问的元素的有序序列),使用List接口的实现是有意义的。

The two most common implementations are ArrayList and LinkedList.

两个最常见的实现是ArrayListLinkedList

Suppose we have the following Lists:

假设我们有以下Lists。

List<String> arrayList = new ArrayList<>();
// populate the ArrayList

List<String> linkedList = new LinkedList<>();
// populate the LinkedList

Since both classes implement the same interface, the example code to remove the first element looks the same:

因为这两个类实现了相同的接口,所以删除第一个元素的示例代码看起来是一样的。

arrayList.remove(0);
linkedList.remove(0);

In the case of ArrayList, the cost of removing is O(n), while LinkedList has a cost of O(1).

ArrayList的情况下,移除的成本是O(n),而LinkedList的成本是O(1)

Now, this doesn’t mean we should use a LinkedList everywhere as the default since the cost for retrieving an object is the other way around. The cost of calling get(i) is O(1) in the case of ArrayList and O(n) in the case of LinkedList.

现在,这并不意味着我们应该到处使用LinkedList作为默认,因为检索一个对象的成本是相反的。在ArrayList的情况下,调用get(i)的成本是O(1),而在LinkedList的情况下是O(n)

4. Conclusion

4.总结

We’ve seen how to remove the first element of an array in Java. In addition, we’ve looked at how to achieve the same result using the Java Collections Framework.

我们已经看到如何在Java中移除数组的第一个元素。此外,我们还研究了如何使用Java集合框架来实现同样的结果。

You can find the example code over on GitHub.

你可以在GitHub上找到示例代码over