java 尝试从 Array.asList 返回的列表中删除时出现 UnsupportedOperationException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1624144/
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-10-29 17:19:22  来源:igfitidea点击:

UnsupportedOperationException when trying to remove from the list returned by Array.asList

javaexceptionlist

提问by Chathuranga Chandrasekara

I am using a Listto hold some data obtained by calling Array.asList()method. Then I am trying to remove an element using myList.Remove(int i)method. But while I try to do that I am getting an UnsupportedOperationException. What would be the reason for this? How should I resolve this problem?

我正在使用 aList来保存通过调用Array.asList()方法获得的一些数据。然后我尝试使用myList.Remove(int i)方法删除一个元素。但是当我尝试这样做时,我得到了一个UnsupportedOperationException. 这是什么原因?我应该如何解决这个问题?

回答by Jon Skeet

Array.asList()wrapsan array in the list interface. The list is still backed by the array. Arrays are a fixed size - they don't support adding or removing elements, so the wrapper can't either.

Array.asList()在列表接口中包装一个数组。该列表仍由数组支持。数组是固定大小的——它们不支持添加或删除元素,因此包装器也不能。

The docs don't make this as clear as they might, but they dosay:

文档没有尽可能清楚地说明这一点,但他们确实说:

Returns a fixed-size list backed by the specified array.

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

The "fixed-size" bit should be a hint that you can't add or remove elements :)

“固定大小”位应该是一个提示,您不能添加或删除元素:)

Although there are other ways around this (other ways to create a new ArrayListfrom an array) without extra libraries, I'd personally recommend getting hold of the Google Collections Library(or Guava, when it's released). You can then use:

尽管有其他方法可以解决此问题(ArrayList从数组创建新的其他方法)而无需额外的库,但我个人建议您使用Google Collections Library(或Guava,当它发布时)。然后您可以使用:

List<Integer> list = Lists.newArrayList(array);

The reason I'm suggesting this is that the GCL is a generally good thing, and well worth using.

我建议这样做的原因是 GCL 通常是一件好事,非常值得使用。

As noted in comments, this takes a copyof the array; the list is not backed by the original array, and changes in either collection will not be seen in the other.

正如评论中所指出的,这需要一个数组的副本;该列表不受原始数组的支持,并且在另一个集合中不会看到任何一个集合中的更改。

回答by Carlos

It's not java.util.ArrayList. Arrays.asList()returns its own Listimplementation (with changes "written through" to the array.).

不是java.util.ArrayListArrays.asList()返回它自己的List实现(更改“写入”到数组。)。

It's a fixed-sizelist so it does not support removal.

这是一个固定大小的列表,因此不支持删除。

You can create a real ArrayListfrom it:

您可以从中创建一个真实的ArrayList

new java.util.ArrayList<>(Arrays.asList(someArray));  

It's very confusing how asList()works, I must admit.

asList()我必须承认,它的工作原理非常令人困惑。

回答by sleske

Please read the API docs for Arrays.asList():

请阅读Arrays.asList()的 API 文档:

Returns a fixed-size list backed by the specified array. (Changes to the returned list "write through" to the array.)

返回由指定数组支持的固定大小列表。(更改返回的列表“直写”到数组。)

Note that Collections.remove(int) is marked in the Javadocs as an "optional operation", meaning not all Collections will support it. "fixed-size list" means you cannot change the list's size, which remove() would do. So it's not supported.

请注意 Collections.remove(int) 在 Javadocs 中被标记为“可选操作”,这意味着并非所有 Collections 都支持它。“固定大小的列表”意味着您不能更改列表的大小,而 remove() 会这样做。所以不支持。

If you want to change the list generated by Arrays.asList(), just copy it, e.g. new ArrayList(Arrays.asList(...)).

如果要更改由 Arrays.asList() 生成的列表,只需复制它,例如new ArrayList(Arrays.asList(...)).

回答by Stefan Ernst

the implementation you receive from asList doesnt implement a full List interface.I would transform the list to ArrayList and then do modifications on it.

您从 asList 收到的实现没有实现完整的 List 接口。我会将列表转换为 ArrayList,然后对其进行修改。

See http://java.sun.com/j2se/1.4.2/docs/api/java/util/List.html#remove%28int%29

http://java.sun.com/j2se/1.4.2/docs/api/java/util/List.html#remove%28int%29

回答by St.Shadow

Because you get read-only list. try

因为你得到只读列表。尝试

List newList = new ArrayList(myList);

回答by shubomb

use

利用

ArrayList instead of List

ArrayList 而不是 List

List has fixed size element, List can neither addition item nor remove item

List 有固定大小的元素,List 既不能添加项也不能删除项