在使用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的更多相关文章

  1. Arrays.asList()后调用add,remove这些method时出现java.lang.UnsupportedOperationException异常

    String[] queryNames = request.getParameterValues("queryName"); List<String> queryNam ...

  2. 【java】在分页查询结果中对最后的结果集List进行操作add()或remove()操作,报错:java.lang.UnsupportedOperationException

    场景: 在分页查询结果中对最后的结果集List进行操作add()或remove()操作,报错:java.lang.UnsupportedOperationException 错误: java.lang ...

  3. java 异常java.lang.UnsupportedOperationException

    在项目中采用一个枚举的集合,本人采用Collections中的空集合Collections.emptyList()在添加时发生异常: 常见集合如下: private List<VacationC ...

  4. 使用Arrays.asList抛出java.lang.UnsupportedOperationException

    使用 Arrays.asList("str1", "str2")生成的List,不能进行remove.add操作,会产生异常java.lang.Unsuppor ...

  5. java.lang.UnsupportedOperationException 原因以及解决方案

    如下代码: Map[] cardProds = JsonUtils.getObject(oldCartValue, new TypeReference<Map[]>(){}); List& ...

  6. Hbase delete遇到的常见异常: Exception in thread "main" java.lang.UnsupportedOperationException

    hbase 执行批量删除时出现错误: Exception in thread "main" java.lang.UnsupportedOperationException at j ...

  7. java.lang.UnsupportedOperationException解决方法!!!

    在项目中对List进行操作时报错java.lang.UnsupportedOperationException,后来发现操作的List是由数组转换而成的,通过看源码发现问题,并写测试程序如下. 代码块 ...

  8. Arrays.asList引起的java.lang.UnsupportedOperationException解决方法

    在项目中对List进行操作时报错java.lang.UnsupportedOperationException,后来发现操作的List是由数组转换而成的,通过看源码发现问题,并写测试程序如下. 代码块 ...

  9. java.lang.UnsupportedOperationException 异常分析

    今天将一个数组转换成 List 然后进行 remove 操作时却抛出 java.lang.UnsupportedOperationException 异常. String pattern = &quo ...

随机推荐

  1. 解决div布局中第一个div的margin-top在浏览器中显示无效果问题。

    原味来源:http://www.hicss.net/do-not-tell-me-you-understand-margin/ 垂直外边距合并问题 别被上面这个名词给吓倒了,简单地说,外边距合并指的是 ...

  2. OC6_字符串练习

    // // QFString.h // OC6_字符串练习 // // Created by zhangxueming on 15/6/10. // Copyright (c) 2015年 zhang ...

  3. JavaScript基础-面向对象编程<2>

    2.动态添加,修改和删除对象属性和方法 例如:用类Object()创建一个空对象user,然后修改其行为. (1) 添加属性 var user=new Object(); //创建一个没有属性和方法的 ...

  4. jQuery 源码分析 8: 回头看jQuery的构造器(jQuery.fn,jQury.prototype,jQuery.fn.init.prototype的分析)

    在第一篇jQuery源码分析中,简单分析了jQuery对象的构造过程,里面提到了jQuery.fn.jQuery.prototype.jQuery.fn.init.prototype的关系. 从代码中 ...

  5. Css颜色定义的方法汇总color属性设置方式

    颜色的定义方式用rgb()里面带上十进制的数字来定义. color:rgb(211,123,135); 用预定义的颜色名称. color:red; rgba()最后一个参数是不透明度. color:r ...

  6. UVA - 297 Quadtrees (四分树)

    题意:求两棵四分树合并之后黑色像素的个数. 分析:边建树边统计. #include<cstdio> #include<cstring> #include<cstdlib& ...

  7. 栈(链式存储) C++模板实现

    #include <iostream> using namespace std; //栈结点类 template <typename T> class stackNode{ p ...

  8. jQuery.hhLRSlider 左右滚动图片插件

    /**  * jQuery.hhLRSlider 左右滚动图片插件  * User: huanhuan  * QQ: 651471385  * Email: th.wanghuan@gmail.com ...

  9. 有关C++ std::string 类的类型转换 其他语言永远无法理解的伤

    最近做了个项目,C++的MFC窗口程序,一个基于dialog的学生-图书管理系统,有一些感触,最后会放上一些项目截图和部分代码提供大家参考.如果有什么好方法和建议欢迎指导. 强类型,为什么这么伤 我知 ...

  10. python 生成排列、组合以及选择

    from <python cookbook> 19.15 任务 需要对一个序列的排列(permutation).组合(combination)或选择(selection)进行迭代操作.即使 ...