一尘不染

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

java

我试过下面的代码

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)

谁能帮我解决这个问题?


阅读 225

收藏
2020-12-03

共1个答案

一尘不染

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

例如:

字符串valuesInArray [] = {“ 1”,“ 2”,“ 3”,“ 4”};  
列表modifiableList = new ArrayList(Arrays.asList(valuesInArray));
System.out.println(modifiableList.remove(“ 1”)+“ remove flag”);  
System.out.println(“ collcetion” + modifiableList);

响应:

真删除标志

集合[2,3,4]

2020-12-03