java.lang.UnsupportedOperationException
来源:互联网 发布:苹果电脑做淘宝方便吗 编辑:程序博客网 时间:2024/06/05 14:08
在使用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。
例:
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.lang.UnsupportedOperationException翻译
- java.lang.UnsupportedOperationException:错误
- java.lang.UnsupportedOperationException
- java.lang.UnsupportedOperationException: createRequestComponentDeployer
- java.lang.UnsupportedOperationException异常
- 【转】java.lang.UnsupportedOperationException
- java.lang.UnsupportedOperationException
- java.lang.UnsupportedOperationException
- java.lang.UnsupportedOperationException异常
- java.lang.UnsupportedOperationException
- java.lang.UnsupportedOperationException
- java.lang.UnsupportedOperationException
- java.lang.UnsupportedOperationException
- java.lang.UnsupportedOperationException
- java.lang.UnsupportedOperationException 解决方案
- java.lang.UnsupportedOperationException
- 异常java.lang.UnsupportedOperationException
- java.lang.UnsupportedOperationException
- Spark入门实战系列--3.Spark编程模型(上)--编程模型及SparkShell实战
- 数据存储
- Android 技术总结(004)—— ScrollView中的组件设置android:layout_height="fill_parent"不起作用的解决办法
- VS下如何实现一个工程具有两个项目, 测试项目B 调用 A项目生成的dll文件
- PWM波(存在问题,待解决)
- java.lang.UnsupportedOperationException
- javascript 原生 模仿 jquery 选择器 代码
- Linux工具使用
- 将txt文件批量转换成pdf格式的方法
- Java 实现导出excel表 POI
- 使用代码和storyboard分别创建UITabBarController
- Android之自定义ContentProvider详解
- linux sed 命令
- 计蒜客 第10题:寻找插入位置