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 ...
随机推荐
- centos 6.4 samba 权限 selinux权限配置
http://www.cnblogs.com/xiaoluo501395377/archive/2013/05/26/3100444.html(参考) SELINUX 策略 配置好samba后, 输入 ...
- vim插件:latex-suite 使用方法
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4030057.html 零.操作快捷键:对于<++>的块,按下ctrl+j即可快速 ...
- html分页
<div class="fy"> <a href="" title="上一页">上一页</a> < ...
- PHPCMS二次开发教程(转)
转自:http://www.cnblogs.com/semcoding/p/3347600.html PHPCMS V9 结构设计 根目录 |–api 结构文件目录 |–caches 缓存文件目录 ...
- CentOS 6.5升级Python后yum不可用的解决方案
因开发需要,今天把CentOS 6.5自带的Python2.6.6升级到了Python2.7.3.按照如下步骤进行升级 1.查看当前系统python的版本 python -V 2.下载2.7.3版本的 ...
- 原生js在IE7下 向dom添加节点的一个bug, (本例为添加hidden input)
需求是要用js向dom结构增加1个hidden用来存放要post到服务器的数据 var aspnetForm = document.getElementById("aspnetForm&qu ...
- 软件测试 -- 和用户共同测试(UAT测试)的注意点有哪些
软件产品在投产前,通常都会进行用户验收测试.如果用户验收测试没有通过,直接结果就是那不到“Money”,间接影响是损害了公司的形象,而后者的影响往往更严重.根据作者的经验,用户验收测试一定要让用户满意 ...
- C#中的lock关键字(初识)
http://kb.cnblogs.com/page/88513/ 首先给出MSDN的定义: lock 关键字可以用来确保代码块完成运行,而不会被其他线程中断.这是通过在代码块运行期间为给定对象获取互 ...
- Cocos2dx坐标转换
Cocos2dx坐标转换 这段时间加班有点猛,没有太多时间来写博客了,ok,继续完成任务: 前言 这里将会重点介绍四个函数: convertToNodeSpace convertToNodeSpace ...
- 我的PHP之旅--数组的认识(初级)
数组 PHP的数组与swift有些许不同,分为3类(初级,以后会涉及到多维数组和数组指针等). 枚举数组 关联数组 混合数组 枚举数组 枚举数组跟swift中的数组差不多: <?php $arr ...