ArrayList Iterator remove java.lang.UnsupportedOperationException
在使用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 com.test;
import java.util.Arrays;
import java.util.List;
public class TestUnsupported {
public static void main(String[] args) {
String[] s = {
"one", "two", "three", "four", "five",
"six", "seven", "eight", "nine", "ten",
};
List a = Arrays.asList(s);
System.out.println(
"a.contains(" + s[0] + ") = " +
a.contains(s[0]));
a.add("eleven"); // Unsupported
a.remove(s[0]); // Unsupported
}
}
运行后,抛出异常如下:
Exception in thread "main" java.lang.UnsupportedOperationException
at java.util.AbstractList.add(AbstractList.java:151)
at java.util.AbstractList.add(AbstractList.java:89)
at com.test.TestUnsupported.main(TestUnsupported.java:28)
解决方法是转换为ArrayList
List arrayList = new ArrayList(a);
或者直接这么写 List arrayList = new ArrayList(Arrays.asList(s));
参考
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).
ArrayList Iterator remove java.lang.UnsupportedOperationException的更多相关文章
- Arrays.asList()后调用add,remove这些method时出现java.lang.UnsupportedOperationException异常
String[] queryNames = request.getParameterValues("queryName"); List<String> queryNam ...
- 【java】在分页查询结果中对最后的结果集List进行操作add()或remove()操作,报错:java.lang.UnsupportedOperationException
场景: 在分页查询结果中对最后的结果集List进行操作add()或remove()操作,报错:java.lang.UnsupportedOperationException 错误: java.lang ...
- java 异常java.lang.UnsupportedOperationException
在项目中采用一个枚举的集合,本人采用Collections中的空集合Collections.emptyList()在添加时发生异常: 常见集合如下: private List<VacationC ...
- 使用Arrays.asList抛出java.lang.UnsupportedOperationException
使用 Arrays.asList("str1", "str2")生成的List,不能进行remove.add操作,会产生异常java.lang.Unsuppor ...
- java.lang.UnsupportedOperationException 原因以及解决方案
如下代码: Map[] cardProds = JsonUtils.getObject(oldCartValue, new TypeReference<Map[]>(){}); List& ...
- Hbase delete遇到的常见异常: Exception in thread "main" java.lang.UnsupportedOperationException
hbase 执行批量删除时出现错误: Exception in thread "main" java.lang.UnsupportedOperationException at j ...
- java.lang.UnsupportedOperationException解决方法!!!
在项目中对List进行操作时报错java.lang.UnsupportedOperationException,后来发现操作的List是由数组转换而成的,通过看源码发现问题,并写测试程序如下. 代码块 ...
- Arrays.asList引起的java.lang.UnsupportedOperationException解决方法
在项目中对List进行操作时报错java.lang.UnsupportedOperationException,后来发现操作的List是由数组转换而成的,通过看源码发现问题,并写测试程序如下. 代码块 ...
- java.lang.UnsupportedOperationException 异常分析
今天将一个数组转换成 List 然后进行 remove 操作时却抛出 java.lang.UnsupportedOperationException 异常. String pattern = &quo ...
随机推荐
- jQuery 源码分析4: jQuery.extend
jQuery.extend是jQuery最重要的方法之一,下面看看jQuery是怎样实现扩展操作的 // 如果传入一个对象,这个对象的属性会被添加到jQuery对象中 // 如果传入两个或多个对象,所 ...
- yiic创建YII应用 "php.exe"不是内部或外部命令 解决办法
第一步:运行CMD命令. 第二步:进入Yiic文件的目录 (例如在D盘里面 D:/yii/framework) 第三步:D:\yii\framework>yiic webapp D: ...
- 顺序表 C++模板实现
#include <iostream> using namespace std; template <typename T> class list{ private: int ...
- source和.命令的区别
source FileName 作用:在当前bash环境下读取并执行FileName中的命令. 注:该命令通常用命令“.”来替代. 如:source .bash_rc 与 . .bash_rc 是等效 ...
- linux共享文件samba安装与java读取外部文件夹方法
测试环境RedHat 6.4 一.安装 samba组件安装: (1)首先用“rpm –qa |grep samba”命令检验系统samba服务是否安装. #rpm –qa |grep samba sa ...
- Python3 内建模块 hashlib、itertools、HTMLParser、urllib
Python的hashlib提供了常见的摘要算法,如MD5,SHA1等等. 什么是摘要算法呢?摘要算法又称哈希算法.散列算法.它通过一个函数,把任意长度的数据转换为一个长度固定的数据串(通常用16进制 ...
- 浅谈MVC、MVP、MVVM架构模式的区别和联系
MVC.MVP.MVVM这些模式是为了解决开发过程中的实际问题而提出来的,目前作为主流的几种架构模式而被广泛使用. 一.MVC(Model-View-Controller) MVC是比较直观的架构模式 ...
- Memcached服务器安装、配置、使用详解
管理memcached服务 启动Memcached 一般情况下,简单地可以使用类似如下形式,启动Memcached服务: /usr/local/bin/memcached -d -m 64 -I 20 ...
- sql触发器知识
触发器中的Inserted和deleted临时表: SQL2000中,inserted表和deleted表用于存放对表中数据行的修改信息.他们是触发器执行时自动创建的,放在内存中,是临时表.当触发器工 ...
- 微信获取用户的openid和详细信息
获取用户的信息的原理,首先用户会点击一个url,这个url会包含一个参数redirect_uri,这个url是指向微信那边的服务器的,然后微信会把这个http请求重定向到redirect_uri,即我 ...