我想将successList设置为[10,14]
我试过的是,
String oneStr = "10,11,12,14";
String twoStr = "11,14";
Returns r = new Returns();
r.setFailureList(Arrays.asList(twoStr.split(",")));
r.setSuccessList(Arrays.asList(oneStr.split(",")));
r.getSuccessList().removeAll(r.getFailureList());
System.out.println(r.getSuccessList());
返回类是,
List<String> successList;
List<String> failureList;
public List<String> getSuccessList() {
return successList;
}
public void setSuccessList(List<String> successList) {
this.successList = successList;
}
public List<String> getFailureList() {
return failureList;
}
public void setFailureList(List<String> failureList) {
this.failureList = failureList;
}
public void setFailureList(ArrayList<String> failureList) {
this.failureList = failureList;
}
并且导致java.lang.UnsupportedOperationException
stackTrace,
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.removeAll(Unknown Source)
答案 0 :(得分:11)
来自asList()
的文档返回由指定数组支持的固定大小的列表。
因此,当您直接修改(添加,删除..etc)时,最终会得到exception
。
所以,但是在案例中
new ArrayList<String>(Arrays.asList(twoStr.split(",")))
此处您不是直接引用list
。您正在创建新的ArrayList
并使用Arrays.as.asList(twoStr.split(","))
返回的列表数据,该数据不是Unmodifiable
(就大小而言)。
答案 1 :(得分:1)
更改
r.setFailureList(Arrays.asList(twoStr.split(",")));
r.setSuccessList(Arrays.asList(oneStr.split(",")));
作为
r.setFailureList(new ArrayList<String>(Arrays.asList(twoStr.split(","))));
r.setSuccessList(new ArrayList<String>(Arrays.asList(oneStr.split(","))));