Java List UnsupportedOperationException – Java List UnsupportedOperationException

最后修改: 2018年 4月 15日

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

1. Overview

1.概述

In this quick tutorial, we’ll discuss a common Exception that can occur when working with some the API of most List implementations – the UnsupportedOperationException.

在这个快速教程中,我们将讨论一个常见的Exception,它可能在使用大多数List实现的一些API时发生,即UnsupportedOperationException

A java.util.List has more functionality than an ordinary array can support. For instance, with only one built-in method call, it’s possible to check if a specific element is inside the structure. That’s typically why we sometimes need to convert an array to a List or Collection.

java.util.List比普通的rray可以支持更多的功能。例如,只需要一个内置的方法调用,就可以检查一个特定的元素是否在结构内。这就是为什么我们有时需要将array转换为ListCollection

For an introduction to the core Java List implementation – the ArrayList – please refer to this article.

关于Java核心List实现–ArrayList的介绍,请参考这篇文章

2. UnsupportedOperationException

2.不支持操作的异常

A frequent way in which this error occurs is when we use asList() method from java.util.Arrays:

这种错误经常发生,当我们使用asList()方法时,java.util.Arrays:

public static List asList(T... a)

It returns:

它返回。

  • a fixed-size List as of size of a given array
  • an element of the same type as the one in the original array and it must be an Object
  • elements in the same order as in original array
  • a list that is serializable and implements RandomAccess

Since T is a varargs, we can pass an array or the items directly as parameters, and the method will create a fixed-size initialized list:

由于T是一个varargs,我们可以直接传递一个数组或项目作为参数,该方法将创建一个固定大小的初始化列表。

List<String> flowers = Arrays.asList("Ageratum", "Allium", "Poppy", "Catmint");

We can also pass an actual array:

我们也可以传递一个实际的数组

String[] flowers = { "Ageratum", "Allium", "Poppy", "Catmint" };
List<String> flowerList = Arrays.asList(flowers);

Since the returned List is a fixed-size List, we can’t add/remove elements.

由于返回的List是一个固定大小的List,我们无法添加/删除元素

An attempt to add more elements would cause UnsupportedOperationException:

试图添加更多的元素将导致不支持的操作异常

String[] flowers = { "Ageratum", "Allium", "Poppy", "Catmint" }; 
List<String> flowerList = Arrays.asList(flowers); 
flowerList.add("Celosia");

The root of this Exception is that the returned object doesn’t implement the add() operation since it isn’t the same as java.util.ArrayList.

这个Exception的根源在于,返回的对象没有实现add() 操作,因为它与java.util.ArrayList不相同。

It’s an ArrayList, from java.util.Arrays.

它是一个ArrayList,来自java.util.Arrays.

Another way to obtain the same exception is by trying to remove an element from the obtained list.

另一种获得相同异常的方法是试图从获得的列表中删除一个元素。

On the other hand, there are ways to obtain a mutable List in case we need it.

另一方面,有一些方法可以获得一个可变的List,以备我们需要。

One of them is to create an ArrayList or any kind of list directly from the result of asList():

其中之一是直接从asList()的结果中创建一个ArrayList或任何种类的列表。

String[] flowers = { "Ageratum", "Allium", "Poppy", "Catmint" }; 
List<String> flowerList = new ArrayList<>(Arrays.asList(flowers));

3. Conclusion

3.总结

In conclusion, it’s important to understand that adding more elements to a list can be problematic for more than just immutable lists.

总之,重要的是要明白,向一个列表添加更多的元素,不仅仅是不可变的列表会有问题。

As always, the full source code of the examples is available over on GitHub.

一如既往,这些示例的完整源代码可在GitHub上获得over