Java 尝试添加另一个列表时 List.addAll 抛出 UnsupportedOperationException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25624251/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 00:49:20  来源:igfitidea点击:

List.addAll throwing UnsupportedOperationException when trying to add another list

java

提问by Batty

List.addAll throwing UnsupportedOperationExceptionwhen trying to add another list.

List.addAllUnsupportedOperationException在尝试添加另一个列表时抛出。

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

and in loop I am doing,

在循环中我正在做,

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

reading supportTypesfrom 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?

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

采纳答案by Eran

Arrays.asListreturns 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 addAllwork :

您可以创建一个可修改的列表来addAll工作:

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

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

回答by Jean Logeart

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(",")));

回答by bsiamionau

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

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

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

Related question:

相关问题:

Arrays.asList() Confusing source code

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

From the documentation:

文档

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

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

回答by Suragch

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<>();

回答by CoolMind

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

在我的情况下,这个异常发生在我调用时adapter.addAll(items)adapter自定义ArrayAdapter. 这个 CustomAdapter 有一个 typeArray而不是 的参数ArrayList