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 ...
随机推荐
- Razor引擎学习:RenderBody,RenderPage和RenderSection
ASP.NET MVC 3 已经正式发布了,现在估计许多人都在拼命学,我也不能例外,刚刚看到了一篇文章,介绍了三个非常有用的方法:RenderBody,RenderPage和RenderSection ...
- SQL Constraint/Index
1.SQL Constraint Integrity Constraints are used to apply business rules for the database tables. The ...
- Menu bar missing from ClearCase Explorer
See following links: Menu bar missing from ClearCase Explorer Understanding the Rational ClearCase E ...
- OpenJudge 2817:木棒 / Poj 1011 Sticks
1.链接地址: http://bailian.openjudge.cn/practice/2817/ http://poj.org/problem?id=1011 2.题目: 总时间限制: 1000m ...
- 《APUE》第三章笔记(4)及习题3-2
APUE第三章的最后面给出的函数,现在还用不着,所以,先留个名字,待到时候用着了再补上好了. dup和dup2函数:用来复制文件描述符的 sync函数,fsync函数和fdatasync函数:大致的功 ...
- 基于Hadoop生态圈的数据仓库实践 —— ETL
使用Hive转换.装载数据 1. Hive简介 (1)Hive是什么 Hive是一个数据仓库软件,使用SQL读.写.管理分布式存储上的大数据集.它建立在Hadoop之上,具有以下功能和 ...
- fsockopen
fsockopen — 打开一个网络连接或者一个Unix套接字连接 说明 resource fsockopen ( string $hostname [, int $port = -1 [, int ...
- About building ant & install ant on centos7 {ant source code 1.94}
hamcrest-junit-2.0.0.0.jar java-hamcrest-2.0.0.0.jar copy to ant-sourceCodeDir/lib/o ...
- hdu 2767 Proving Equivalences
Proving Equivalences 题意:输入一个有向图(强连通图就是定义在有向图上的),有n(1 ≤ n ≤ 20000)个节点和m(0 ≤ m ≤ 50000)条有向边:问添加几条边可使图变 ...
- nodejs基础安装
安装Nodejs需要从官网上下载一个最新的安装包,运行.我这里是win764位系统. 下载版本6.5.0 由于去外国的镜像上下载东西比较慢,淘宝为我们准备了国内的镜像.我们需要安装国内镜像的使用工具. ...