首页 > ArrayList Iterator remove java.lang.UnsupportedOperationException

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).

转载于:https://www.cnblogs.com/toSeeMyDream/p/5253794.html

更多相关:

  • 二、数组列表 —— ArrayList      1、构造方法   ArrayList 是 Java 中的动态数组,底层实现就是对象数组,只不过数组的容量会根据情况来改变。   它有个带 int 类型参数的构造方法,根据传入的参数,扩展初始化的数组容量,这个方法是推荐使用的,因为如果预先知道数组的容量,可以设置好初始值,而不用等每次...

  • 题目: Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For example, If n = 4 and k = 2, a solution is: [[2,4],[3,4],[2,3],[1,...

  • 上篇笔记中梳理了一把 resolver 和 balancer,这里顺着前面的流程走一遍入口的 ClientConn 对象。ClientConn// ClientConn represents a virtual connection to a conceptual endpoint, to // perform RPCs. // //...

  • 我的实验是基于PSPNet模型实现二维图像的语义分割,下面的代码直接从得到的h5文件开始往下做。。。 也不知道是自己的检索能力出现了问题还是咋回事,搜遍全网都没有可以直接拿来用的语义分割代码,东拼西凑,算是搞成功了。 实验平台:Windows、VS2015、Tensorflow1.8 api、Python3.6 具体的流程为:...

  • Path Tracing 懒得翻译了,相信搞图形学的人都能看得懂,2333 Path Tracing is a rendering algorithm similar to ray tracing in which rays are cast from a virtual camera and traced through a s...

  • configure_file( [COPYONLY] [ESCAPE_QUOTES] [@ONLY][NEWLINE_STYLE [UNIX|DOS|WIN32|LF|CRLF] ]) 我遇到的是 configure_file(config/config.in ${CMAKE_SOURCE_DIR}/...

  •     直接复制以下代码创建一个名为settings.xml的文件,放到C:UsersAdministrator.m2下即可