List.addAll在尝试添加其他列表时抛出UnsupportedOperationException

时间:2021-12-25 09:24:59

List.addAll throwing UnsupportedOperationException when trying to add another list.

List.addAll在尝试添加其他列表时抛出UnsupportedOperationException。

List<String> supportedTypes = Arrays.asList("6500", "7600"};

and in loop I am doing,

我在做循环

supportedTypes.addAll(Arrays.asList(supportTypes.split(","))); //line 2

reading supportTypes from a file.

从文件中读取supportTypes。

But line 2 throws a UnsupportedOperationException, but I am not able to determine why?

但第2行抛出UnsupportedOperationException,但我无法确定原因?

I am adding another list to a list, then why this operation is unsupported?

我在列表中添加另一个列表,那么为什么不支持此操作?

5 个解决方案

#1


59  

Arrays.asList returns a fixed sized list backed by an array, and you can't add elements to it.

Arrays.asList返回由数组支持的固定大小的列表,您无法向其添加元素。

You can create a modifiable list to make addAll work :

您可以创建可修改的列表以使addAll工作:

List<String> supportedTypes = new ArrayList<String>(Arrays.asList("6500", "7600", "8700"));

List supportedTypes = new ArrayList (Arrays.asList(“6500”,“7600”,“8700”));

#2


7  

Arrays.asList returns a fixed-size list.

Arrays.asList返回固定大小的列表。

If you to want be able to add elements to the list, do:

如果您希望能够向列表中添加元素,请执行以下操作:

List<String> supportedTypes = new ArrayList<>(Arrays.asList("6500", "7600"});
supportedTypes.addAll(Arrays.asList(supportTypes.split(",")));

#3


6  

This error also happens when the list is initialized with Collections.emptyList(), which is immutable:

使用Collections.emptyList()初始化列表时也会发生此错误,该列表是不可变的:

List<String> myList = Collections.emptyList();

Instead, initialize it with a mutable list. For example

而是使用可变列表初始化它。例如

List<String> myList = new ArrayList<>();

#4


4  

Problem is that Arrays.asList method returns instance of java.util.Arrays.ArrayList which doesn't support add/remove operations on elements. It's not surprizing that addAll method throws exception because add method for java.util.Arrays.ArrayList is defined as:

问题是Arrays.asList方法返回java.util.Arrays.ArrayList的实例,它不支持对元素的添加/删除操作。 addAll方法抛出异常并不令人惊讶,因为java.util.Arrays.ArrayList的add方法定义为:

public void add(int index, E element) {
    throw new UnsupportedOperationException();
}

Related question:

Arrays.asList() Confusing source code

Arrays.asList()令人困惑的源代码

From the documentation:

从文档:

Arrays.asList returns a fixed-size list backed by the specified array.

Arrays.asList返回由指定数组支持的固定大小的列表。

#5


0  

In my case this exception occured when I called adapter.addAll(items), where adapter was a custom ArrayAdapter. This CustomAdapter had a parameter of type Array instead of ArrayList.

在我的情况下,当我调用adapter.addAll(items)时发生了这个异常,其中adapter是一个自定义的ArrayAdapter。此CustomAdapter具有Array类型的参数而不是ArrayList。

#1


59  

Arrays.asList returns a fixed sized list backed by an array, and you can't add elements to it.

Arrays.asList返回由数组支持的固定大小的列表,您无法向其添加元素。

You can create a modifiable list to make addAll work :

您可以创建可修改的列表以使addAll工作:

List<String> supportedTypes = new ArrayList<String>(Arrays.asList("6500", "7600", "8700"));

List supportedTypes = new ArrayList (Arrays.asList(“6500”,“7600”,“8700”));

#2


7  

Arrays.asList returns a fixed-size list.

Arrays.asList返回固定大小的列表。

If you to want be able to add elements to the list, do:

如果您希望能够向列表中添加元素,请执行以下操作:

List<String> supportedTypes = new ArrayList<>(Arrays.asList("6500", "7600"});
supportedTypes.addAll(Arrays.asList(supportTypes.split(",")));

#3


6  

This error also happens when the list is initialized with Collections.emptyList(), which is immutable:

使用Collections.emptyList()初始化列表时也会发生此错误,该列表是不可变的:

List<String> myList = Collections.emptyList();

Instead, initialize it with a mutable list. For example

而是使用可变列表初始化它。例如

List<String> myList = new ArrayList<>();

#4


4  

Problem is that Arrays.asList method returns instance of java.util.Arrays.ArrayList which doesn't support add/remove operations on elements. It's not surprizing that addAll method throws exception because add method for java.util.Arrays.ArrayList is defined as:

问题是Arrays.asList方法返回java.util.Arrays.ArrayList的实例,它不支持对元素的添加/删除操作。 addAll方法抛出异常并不令人惊讶,因为java.util.Arrays.ArrayList的add方法定义为:

public void add(int index, E element) {
    throw new UnsupportedOperationException();
}

Related question:

Arrays.asList() Confusing source code

Arrays.asList()令人困惑的源代码

From the documentation:

从文档:

Arrays.asList returns a fixed-size list backed by the specified array.

Arrays.asList返回由指定数组支持的固定大小的列表。

#5


0  

In my case this exception occured when I called adapter.addAll(items), where adapter was a custom ArrayAdapter. This CustomAdapter had a parameter of type Array instead of ArrayList.

在我的情况下,当我调用adapter.addAll(items)时发生了这个异常,其中adapter是一个自定义的ArrayAdapter。此CustomAdapter具有Array类型的参数而不是ArrayList。