java.lang.UnsupportedOperationException
2015-09-09 14:38
609 查看
在使用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。
例:
解决方法是使用Iterator,或者转换为ArrayList
List arrayList = new ArrayList(list);
参考:
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).
UnsupportedOperationException而且不作任何操作。ArrayList override这些method来对list进行操作,但是Arrays$ArrayList没有override remove(),add()等,所以throw UnsupportedOperationException。
例:
public static void main(String [] args){ List<String> list = Arrays.asList("a","b"); list.add("c"); System.out.println(list); }
解决方法是使用Iterator,或者转换为ArrayList
List arrayList = new ArrayList(list);
参考:
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 实现导出excel表 POI
- Java中ArrayList和LinkedList区别
- spring boot 集成框架事例
- java--xml
- Spring AOP和Servlet使用问题
- Java synchronized详解 二
- Java synchronized详解 一
- java创建多线程的三种方式
- 源码:从控制台接收数据
- eclipse 安装svn插件
- Eclipse:Cannot complete the install because of a conflicting dependency.问题解决
- java开源资源
- java游戏框架那点事(转)
- 源码:打印错误信息和调试信息
- EL&struts2标签 读取map,list集合
- java保留两位小数【转】
- JAVA 计算根据经纬度计算距离
- ti info找不到JAVA_HOME
- Eclipse 中 Ant 无日志输出
- Spring3.0 AOP 详解