Arrays.asList vs new ArrayList(Arrays.asList()) – Arrays.asList vs new ArrayList(Arrays.asList())

最后修改: 2020年 9月 17日

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

1. Overview

1.概述

In this short tutorial, we’ll take a look at the differences between Arrays.asList(array) and ArrayList(Arrays.asList(array)).

在这个简短的教程中,我们将看看Arrays.asList(array)ArrayList(Arrays.asList(array))之间的区别。

2. Arrays.asList

2.Arrays.asList

Let’s start with the Arrays.asList method.

让我们从Arrays.asList方法开始。

Using this method, we can convert from an array to a fixed-size List object. This List is just a wrapper that makes the array available as a list. No data is copied or created.

使用这个方法,我们可以从一个数组转换为一个固定大小的List对象这个列表只是一个包装器,使数组可以作为一个列表使用。没有数据被复制或创建

Also, we can’t modify its length because adding or removing elements is not allowed.

另外,我们不能修改其长度,因为添加或删除元素是不允许的

However, we can modify single items inside the array. Note that all the modifications we make to the single items of the List will be reflected in our original array:

然而,我们可以修改数组中的单个项。请注意,我们对List中的单个项所做的所有修改都将反映在我们原来的数组中。

String[] stringArray = new String[] { "A", "B", "C", "D" };
List stringList = Arrays.asList(stringArray);

Now, let’s see what happens if we modify the first element of stringList:

现在,让我们看看如果我们修改stringList的第一个元素会怎样。

stringList.set(0, "E");
 
assertThat(stringList).containsExactly("E", "B", "C", "D");
assertThat(stringArray).containsExactly("E", "B", "C", "D");

As we can see, our original array was modified, too. Both the list and the array now contain exactly the same elements in the same order.

我们可以看到,我们原来的数组也被修改了。列表和数组现在都包含了完全相同的元素,而且顺序相同。

Let’s now try to insert a new element to stringList:

现在让我们尝试在stringList中插入一个新元素。

stringList.add("F");
java.lang.UnsupportedOperationException
	at java.base/java.util.AbstractList.add(AbstractList.java:153)
	at java.base/java.util.AbstractList.add(AbstractList.java:111)

As we can see, adding/removing elements to/from the List will throw java.lang.UnsupportedOperationException.

正如我们所看到的,向/从List添加/删除元素将抛出java.lang.UnsupportedOperationException

3. ArrayList(Arrays.asList(array))

3.ArrayList(Arrays.asList(array))

Similar to the Arrays.asList method, we can use ArrayList<>(Arrays.asList(array)) when we need to create a List out of an array.

Arrays.asList方法类似,当我们需要从一个数组中创建一个List时,我们可以使用ArrayList<>(Arrays.asList(array))

But, unlike our previous example, this is an independent copy of the array, which means that modifying the new list won’t affect the original array. Additionally, we have all the capabilities of a regular ArrayList, like adding and removing elements:

但是,与我们之前的例子不同,这是一个独立的数组副本,这意味着修改新的列表不会影响原始数组。此外,我们拥有普通数组列表的所有功能,比如添加和删除元素。

String[] stringArray = new String[] { "A", "B", "C", "D" }; 
List stringList = new ArrayList<>(Arrays.asList(stringArray));

Now let’s modify the first element of stringList:

现在我们来修改stringList的第一个元素。

stringList.set(0, "E");
 
assertThat(stringList).containsExactly("E", "B", "C", "D");

And now, let’s see what happened with our original array:

现在,让我们看看我们的原始阵列发生了什么。

assertThat(stringArray).containsExactly("A", "B", "C", "D");

As we can see, our original array remains untouched.

正如我们所看到的,我们的原始阵列仍然没有被触动

Before wrapping up, if we take a look at the JDK source code, we can see the Arrays.asList method returns a type of ArrayList that is different from java.util.ArrayList. The main difference is that the returned ArrayList only wraps an existing array — it doesn’t implement the add and remove methods.

在结束之前,如果我们看一下JDK 源代码,我们可以看到Arrays.asList方法返回一个ArrayList的类型,它与java.util.ArrayList不同。主要的区别在于,返回的ArrayList只包裹了一个现有的数组–它没有实现addremove方法。

4. Conclusion

4.总结

In this short article, we took a look at the differences between two ways of converting an array into an ArrayList. We saw how those two options behave and the difference between how they implement their internal arrays.

在这篇短文中,我们看了将数组转换为ArrayList的两种方式的区别。我们看到了这两种方式的表现,以及它们实现内部数组的方式之间的区别。

As always, the code samples can be found over on GitHub.

一如既往,代码样本可以在GitHub上找到over