Remove the First Element from a List – 移除列表中的第一个元素

最后修改: 2018年 7月 27日

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

1. Overview

1.概述

In this super-quick tutorial, we’ll show how to remove the first element from a List.

在这个超级快速的教程中,我们将展示如何从一个List中移除第一个元素。

We’ll perform this operation for two common implementations of the List interface – ArrayList and LinkedList.

我们将对List接口的两种常见实现–ArrayListLinkedList进行这一操作。

2. Creating a List

2.创建一个列表

Firstly, let’s populate our Lists:

首先,让我们填充我们的Lists。

@Before
public void init() {
    list.add("cat");
    list.add("dog");
    list.add("pig");
    list.add("cow");
    list.add("goat");

    linkedList.add("cat");
    linkedList.add("dog");
    linkedList.add("pig");
    linkedList.add("cow");
    linkedList.add("goat");
}

3. ArrayList

3.ArrayList

Secondly, let’s remove the first element from the ArrayList, and make sure that our list doesn’t contain it any longer:

其次,让我们从ArrayList,中删除第一个元素,并确保我们的列表不再包含它。

@Test
public void givenList_whenRemoveFirst_thenRemoved() {
    list.remove(0);

    assertThat(list, hasSize(4));
    assertThat(list, not(contains("cat")));
}

As shown above, we’re using remove(index) method to remove the first element – this will also work for any implementation of the List interface.

如上所示,我们使用remove(index)方法来删除第一个元素–这也将适用于任何List接口的实现。

4. LinkedList

4.LinkedList

LinkedList also implements remove(index) method (in its own way) but it has also the removeFirst() method.

LinkedList也实现了remove(index)方法(以自己的方式),但它也有removeFirst()方法。

Let’s make sure that it works as expected:

让我们确保它按预期工作。

@Test
public void givenLinkedList_whenRemoveFirst_thenRemoved() {
    linkedList.removeFirst();

    assertThat(linkedList, hasSize(4));
    assertThat(linkedList, not(contains("cat")));
}

5. Time Complexity

5.时间的复杂性

Although the methods look similar, their efficiency differs. ArrayList‘s remove() method requires O(n) time, whereas LinkedList‘s removeFirst() method requires O(1) time.

虽然这些方法看起来很相似,但是它们的效率却不同。ArrayListremove()方法需要O(n)时间,而LinkedListremoveFirst()方法需要O(1)时间。

This is because ArrayList uses an array under the hood, and the remove() operation requires copying the rest of the array to the beginning. The larger the array is, the more elements need to be shifted.

这是因为ArrayList在引擎盖下使用了一个数组,而remove()操作需要将数组的其余部分复制到开头。数组越大,需要移位的元素就越多。

Unlike that, LinkedList uses pointers meaning that each element points to the next and the previous one.

与此不同,LinkedList使用指针,意味着每个元素都指向下一个和上一个元素。

Hence, removing the first element means just changing the pointer to the first element. This operation always requires the same time not depending on the size of a list.

因此,删除第一个元素意味着只是改变第一个元素的指针。这个操作总是需要相同的时间,不取决于列表的大小。

6. Conclusion

6.结论

In this article, we’ve covered how to remove the first element from a List, and have compared the efficiency of this operation for ArrayList and LinkedList implementations.

在这篇文章中,我们介绍了如何从List中移除第一个元素,并比较了ArrayListLinkedList实现的这一操作的效率。

As usual, the complete source code is available over on GitHub.

像往常一样,完整的源代码可以在GitHub上找到