Java 数组转换成List,然后执行add或remove抛异常UnsupportedOperationException问题的解决
来源:互联网 发布:仁和知柏地黄丸的功效 编辑:程序博客网 时间:2024/05/29 07:13
在使用Arrays.asList()后调用add,remove这些method时出现java.lang.UnsupportedOperationException异常。这是由于Arrays.asList() 返回java.util.Arrays$ArrayList, 而不是ArrayList。Arrays$ArrayList和ArrayList都是继承AbstractList,remove,add等method在AbstractList中是默认throw UnsupportedOperationException而且不作任何操作。ArrayList override这些method来对list进行操作,但是Arrays$ArrayList没有override remove(),add()等,所以throw UnsupportedOperationException。
例子:
package CloudPanSys_Test;import java.util.Arrays;import java.util.List;public class Spilt {public static void main(String[] args) {String path = "/root/m/123.txt";path = getClientPath(path);}public static String getClientPath(String svrPath) {StringBuilder newpath = new StringBuilder();String[] svrPaths = svrPath.split("/");List<String> list = Arrays.asList(svrPaths);list.remove(0);//执行抛出异常for (String string : list) {newpath.append("/" + string);}return newpath.toString();}}
运行后,抛出异常如下:
解决
使用Iterator,或者转换为ArrayList
List list = Arrays.asList(svrPaths);List<String> list2 = new ArrayList<String>(list);list2.remove(0);
参考
When you call Arrays.asList it does not return a java.util.ArrayList. It returns a java.util.Arrays$ArrayList which is an immutable list. You cannot add to it and you cannot remove from it.
If you want a mutable list built from your array you will have to loop over the array yourself and add each element into the list in turn.
Even then your code won't work because you'll get an IndexOutOfBoundsException as you remove the elements from the list in the for loop. There are two options: use an Iterator which allows you to remove from the list as you iterate over it (my recommendation as it makes the code easier to maintain) or loop backwards over the loop removing from the last one downwards (harder to read).
You are using AbstractList. ArrayList and Arrays$ArrayList are both types of AbstractList. That's why you get UnsupportedOperationException: Arrays$ArrayList does not override remove(int) so the method is called on the superclass, AbstractList, which is what is throwing the exception because this method is not implemented on that class (the reason being to allow you to build immutable subclasses).
- Java 数组转换成List,然后执行add或remove抛异常UnsupportedOperationException问题的解决
- Java 数组转换成List,然后执行add或remove抛异常UnsupportedOperationException问题的解决
- Java数组转换List后使用用add,remove抛UnsupportedOperationException异常
- 数组转换为List后add或remove出现UnsupportedOperationException异常分析
- 数组转换为List后add或remove出现UnsupportedOperationException异常分析
- 数组转换为List(Arrays.asList)后add或remove出现UnsupportedOperationException
- 数组转换为List(Arrays.asList)后add或remove出现UnsupportedOperationException
- 数组转换为List(Arrays.asList)后add或remove出现UnsupportedOperationException
- Arrays.asList(strArray)转换为List,执行remove(),报异常:java.lang.UnsupportedOperationException深究
- Arrays.asList() 得到 集合 执行 add() 和 remove() 抛 UnsupportedOperationException 异常
- java 异常java.lang.UnsupportedOperationException ---EmptyList此集合竟然没有相应的add,remove等方法
- String数组转List,java.lang.UnsupportedOperationException异常
- List在调用add、remove方法后报java.lang.UnsupportedOperationException
- Java中List.remove报错UnsupportedOperationException
- Java中List.remove报错UnsupportedOperationException
- List进行remove和add操作时缘何会报UnsupportedOperationException
- list执行remove和add方法时,报java.util.ConcurrentModificationException的原因
- List报 java.lang.UnsupportedOperationException异常
- 【数据库Mysql】——存储引擎
- redis持久化RDB和AOF
- linux虚拟机3种网络模式
- Java如何找出数组中前k个高频元素
- LeetCode[126.Word Ladder II]题解 难度[hard]
- Java 数组转换成List,然后执行add或remove抛异常UnsupportedOperationException问题的解决
- Struts 2.5.2 创建一个简单的项目--登录
- 如何实现servlet和struts的友好共存
- 文件操作案例
- 阅读郭林《第一行代码》的笔记——第7章 跨程序共享数据,探究内容提供器
- php字符串位运算的一个小技巧
- URDF文件构建中坐标系
- freeMarker生成word文档
- JSTL标签库之C标签的使用