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 ...
随机推荐
- Java多线程的安全问题
/*多线程的安全问题1.为什么会出现安全问题?因为程序在运行时,会出现一个线程在判断条件满足后,具备了执行资格,但没有运行代码后一个线程也判断了条件,也具备了执行资格,后一个线程运行了代码,但这时候, ...
- vbscript multiple line syntax
Vbscript 如何将输出内容换行? ' VbCrLf represetns Carriage return–linefeed combination, for more information s ...
- hdu 1063 Exponentiation (高精度小数乘法)
//大数继续,额,要吐了. Problem Description Problems involving the computation of exact values of very large m ...
- php header示例代码(推荐)
<?php /*** Function: PHP header() examples (PHP) ** Desc: Some examples on how to use the header( ...
- mysql主配置文件my.cnf详细说明
MySQL配置文件my.cnf 例子最详细翻译,可以保存做笔记用[转载]#BEGIN CONFIG INFO#DESCR: 4GB RAM, 只使用InnoDB, ACID, 少量的连接, 队列负载大 ...
- 【pano2vr】网页Flash中简单实现炫酷的3D模型制作
花了两天时间学习如何能够高效的实现3D模型效果,毕竟是从0开始学习,感觉pano2vr这款软件挺容易上手,并且可以很容易实现简单的热点交互,可以根据交互需求设置皮肤,故将这一款软件推荐给大家: 1.简 ...
- inline-block的兼容性问题
inline-block的兼容性问题 Inline-block是元素display属性的一个值.这个名字的由来是因为,display设置这个值的元素,兼具行内元素( inline elements)跟 ...
- php查询ip地址来源归属地的脚本
<?php header('Content-Type:text/html;charset=utf-8'); if($_GET['sub']){ $ip = $_GET['ip']; $msg = ...
- (转载)使用ADOConnet.BeginTrans后,出现错误提示:无法在此会话中启动更多的事务?
Q: 三层结构,在服务器端使用adoconnection连接到sqlserver2000,然后想在 datasetprovider的beforupdaterecord中使用语句: try adocon ...
- [转载]常用Web Service汇总(天气预报、时刻表等)
下面总结了一些常用的Web Service,是平时乱逛时收集的,希望对大家有用. ============================================ 天气预报Web Servic ...