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
List.addAll throwing UnsupportedOperationException when trying to add another list
提问by Batty
List.addAll throwing UnsupportedOperationException
when 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.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<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.asList
method returns instance of java.util.Arrays.ArrayListwhich 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
方法抛出异常并不奇怪,因为 add 方法java.util.Arrays.ArrayList
定义为:
public void add(int index, E element) {
throw new UnsupportedOperationException();
}
Related question:
相关问题:
Arrays.asList() Confusing source code
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 adapter
was a custom ArrayAdapter
. This CustomAdapter had a parameter of type Array
instead of ArrayList
.
在我的情况下,这个异常发生在我调用时adapter.addAll(items)
,adapter
自定义ArrayAdapter
. 这个 CustomAdapter 有一个 typeArray
而不是 的参数ArrayList
。