Exception in thread "main" java.lang.UnsupportedOperationException: remove
时间: 2023-10-12 20:57:21 浏览: 219
java 操作错误(UnsupportedOperationException)
回答: 引发异常"Exception in thread "main" java.lang.UnsupportedOperationException: remove"的原因是在调用Arrays.asList()方法生成的List对象上调用了add或remove方法。\[2\]Arrays.asList()返回的是Arrays的内部类ArrayList,而不是java.util.ArrayList。Arrays的内部类ArrayList继承自AbstractList,而AbstractList中的remove和add方法默认会抛出UnsupportedOperationException异常。\[3\]解决这个问题的方法是将Arrays.asList()生成的List对象转换为java.util.ArrayList对象,然后再进行add或remove操作。例如,可以使用以下代码解决这个问题:
```
String\[\] array = {"1","2","3","4","5"};
List<String> list = Arrays.asList(array);
List<String> arrList = new ArrayList<>(list);
arrList.add("6");
```
#### 引用[.reference_title]
- *1* *3* [java.lang.UnsupportedOperationException解决方法!!!](https://blog.csdn.net/lcdaaaa/article/details/80240030)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item]
- *2* [java:Exception in thread “main“ java.lang.UnsupportedOperationException](https://blog.csdn.net/qq_44732146/article/details/125866796)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
阅读全文