CrazyAirhead

疯狂的傻瓜,傻瓜也疯狂——傻方能执著,疯狂才专注!

0%

List.addAll抛出UnsupportedOperationException

  • java
  • list

问题

1
2
3
4
5
6
7
String to = "1;2;3";
String cc = "1;2;3";
String[] toSplit = to.split(";");
List<String> list = Arrays.asList(toSplit);
String[] ccSplit = cc.split(";");
List<String> temp = Arrays.asList(ccSplit);
list.addAll(temp);

原因

List.addAll的文档中说,如果列表不支持时,抛出UnsupportedOperationException。

Arrays.asList返回的是一个定长的列表,不能往这个列表添加数据。

解决

1
List<String> list = new ArrayList<>(Arrays.asList(toSplit));

参考链接https://stackoverflow.com/questions/25624251/list-addall-throwing-unsupportedoperationexception-when-trying-to-add-another-li

欢迎关注我的其它发布渠道