java.util.AbstractList.remove 处的 java.lang.UnsupportedOperationException(来源未知)

发布于 2024-12-04 02:24:44 字数 524 浏览 0 评论 0原文

我已经尝试过下面的代码

String s[]={"1","2","3","4"};  
Collection c=Arrays.asList(s);  
System.out.println(c.remove("1") +"  remove flag");  

System.out.println(" collcetion "+c);  

Exception in thread "main" java.lang.UnsupportedOperationException  
at java.util.AbstractList.remove(Unknown Source)  
at java.util.AbstractList$Itr.remove(Unknown Source)  
at java.util.AbstractCollection.remove(Unknown Source)  
at test.main(test.java:26)  

有人可以帮我解决这个问题吗?

I have tried below code

String s[]={"1","2","3","4"};  
Collection c=Arrays.asList(s);  
System.out.println(c.remove("1") +"  remove flag");  

System.out.println(" collcetion "+c);  

I was getting

Exception in thread "main" java.lang.UnsupportedOperationException  
at java.util.AbstractList.remove(Unknown Source)  
at java.util.AbstractList$Itr.remove(Unknown Source)  
at java.util.AbstractCollection.remove(Unknown Source)  
at test.main(test.java:26)  

Can anyone help me to solve this issue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

思念满溢 2024-12-11 02:24:44

简单的解决方法是将 List 传递到 ArrayList 的构造函数中。

例如:

String valuesInArray[]={"1","2","3","4"};  
List modifiableList = new ArrayList(Arrays.asList(valuesInArray));
System.out.println(modifiableList.remove("1") + "  remove flag");  
System.out.println(" collcetion "+ modifiableList); 

响应:

真正删除标志

集合 [2, 3, 4]

Easy work around is just to pass in the List into an ArrayList's constructor.

For example:

String valuesInArray[]={"1","2","3","4"};  
List modifiableList = new ArrayList(Arrays.asList(valuesInArray));
System.out.println(modifiableList.remove("1") + "  remove flag");  
System.out.println(" collcetion "+ modifiableList); 

Response:

true remove flag

collcetion [2, 3, 4]

沉溺在你眼里的海 2024-12-11 02:24:44

稍微更正:不,它不是一个不可修改的集合。它只是不支持添加和删除元素,因为它由提供的数组支持,并且数组不可调整大小。但它支持类似 list.set(索引, 元素)

Slight correction: no, it's not an unmodifiable Collection. It just doesn't support adding and removing elements, because it is backed by the supplied array and arrays aren't resizeable. But it supports operations like list.set(index, element)

药祭#氼 2024-12-11 02:24:44

我遇到了这个问题,因为我也在使用 Arrays.asList 初始化列表:

List<String> names = Arrays.asList("a", "b", "c");

为了解决这个问题,我使用了 addAll 相反:

List<String> names = new ArrayList<String>();
names.addAll(Arrays.asList("a", "b", "c"));

这样您可以编辑列表、添加新项目或删除。

I was having this problem, because I was also initializing my list with Arrays.asList:

List<String> names = Arrays.asList("a", "b", "c");

To solve the problem, I used addAll instead:

List<String> names = new ArrayList<String>();
names.addAll(Arrays.asList("a", "b", "c"));

This way you can edit the list, add new items or remove.

纵性 2024-12-11 02:24:44

java.util.Arrays 类的 Arrays.asList 方法返回的 List 是一个固定大小的列表对象,这意味着不能向列表中添加或删除元素。

因此,诸如添加或删除之类的功能无法在此类列表上进行操作。

添加或删除而不获取 java.lang.UnsupportedOperationException 的解决方案是 ->

List<String> strList= new ArrayList<>(Arrays.asList(strs));

//Then Add or Remove can be called on such List

newList.add("100");
newList.remove("100");

The List returned by Arrays.asList method of java.util.Arrays class is a fixed-size list object which means that elements cannot be added to or removed from the list.

So functions like Adding or Removing cannot be operated on such kind of Lists.

The solution to adding or removing without getting java.lang.UnsupportedOperationException is ->

List<String> strList= new ArrayList<>(Arrays.asList(strs));

//Then Add or Remove can be called on such List

newList.add("100");
newList.remove("100");

尹雨沫 2024-12-11 02:24:44

一个简单的解决办法是像这样声明你的列表:

List<Integer> list2 = new ArrayList<>(Arrays.asList(0,8,1,5,7,0));

A one liner fix is to declare your list like this:

List<Integer> list2 = new ArrayList<>(Arrays.asList(0,8,1,5,7,0));
你不是我要的菜∠ 2024-12-11 02:24:44

以下方法

private void printCollection() {
  List<String> strings = new ArrayList<>(Arrays.asList("1", "2", "3", "4"));

  System.out.println(strings.remove("1") + " remove flag");
  System.out.println("collection " + strings);
}

将打印

true remove flag
collection [2, 3, 4]

注意,trueremove() 的(成功)返回值。

The following method

private void printCollection() {
  List<String> strings = new ArrayList<>(Arrays.asList("1", "2", "3", "4"));

  System.out.println(strings.remove("1") + " remove flag");
  System.out.println("collection " + strings);
}

Will print

true remove flag
collection [2, 3, 4]

Note that true is the (successful) return value from remove().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文