`
dxm1986
  • 浏览: 435027 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

java.lang.UnsupportedOperationException

    博客分类:
  • java
 
阅读更多

  在使用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)

 

解决方法是使用Iterator,或者转换为ArrayList

List arrayList = new ArrayList(a);

参考

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

分享到:
评论
1 楼 長风破浪 2013-07-26  
用Iterator怎么弄,迭代器只能单项移动,且不能add

相关推荐

    java.lang.UnsupportedOperationException异常(csdn)————程序.pdf

    `java.lang.UnsupportedOperationException`是Java中的一个运行时异常,它属于`RuntimeException`的子类。这个异常通常在尝试调用一个不支持的操作时抛出。在Java编程中,某些方法可能在特定对象或特定条件下不支持...

    Android 兼容性问题:java.lang.UnsupportedOperationException解决办法

    "Android 兼容性问题:java.lang.UnsupportedOperationException解决办法" Android 兼容性问题:java.lang.UnsupportedOperationException解决办法是 Android 开发中常见的一种问题。该问题会导致应用程序崩溃,...

    Android webveiw 出现栈错误解决办法

    然而,有时在使用WebView时,可能会遇到一些错误,比如“java.lang.UnsupportedOperationException: For security reasons, WebView is not allowed in privileged processes”。这个错误通常发生在尝试在一个具有高...

    list使用过程中遇到的坑

    xception in thread "main" java.lang.UnsupportedOperationException Arrays.asList转化基本数据类型数组的时候有个意想不到的坑 当我们在实际业务开发过程中,难免会遇到数组转List的操作,通常我们所选择的就是...

    1 ElasticSearch 安装

    ### Elasticsearch在Linux系统的安装与配置详解 #### 一、引言 Elasticsearch是一款基于Lucene的开源搜索和分析引擎,适用于全文检索、结构化数据检索等场景。它以其高性能、高扩展性和易于使用的特性而受到广泛...

    prueba-javassist:用于动态代码插入的字节码级别的类操作测试

    测试javassist 用于动态代码插入的字节码级别的类操作测试虚拟机参数-agentlib:jdwp = ... 根据您运行的 JVM,可能会出现以下错误:线程“HotSwap”中的异常 java.lang.UnsupportedOperationException:未实现架构更改

    hibernate_常见问题集锦.doc

    java.lang.UnsupportedOperationException: The user must supply a JDBC connection ``` **解决方案:** 为了解决这个问题,开发者应该在初始化Configuration对象时,指定Hibernate的配置文件路径,以便读取正确的...

    MockDialogInterface.rar_All You Need

    A mock {@link android.content.DialogInterface} class. All methods are non-functional and throw {@link java.lang.UnsupportedOperationException}. Override it to provide the operations that you need.

    环信移动客服DEMO无问题版

    > com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: java.lang.UnsupportedOperationException 解决这个问题

    Proguard v5.3.3.rar

    代码混淆的时候出现java.lang.UnsupportedOperationException: Unsupported class version number [52.0] (maximum 51.0, Java 1.7),原因是proguard.jar版本太低,需要5.0以上的版本

    cxf冲突_0001

    java.lang.UnsupportedOperationException: This class does not support SAAJ 1.1 此异常表明WebLogic服务器不支持SAAJ 1.1标准。要解决这个问题,可以采取以下步骤: 1. 打开`startWebLogic.cmd`或`start...

    ssh 学习笔记

    在遇到异常时,如java.lang.UnsupportedOperationException和java.lang.NoSuchMethodException,通常表明了代码中可能存在API使用错误、框架配置不当或者依赖版本不一致等问题。例如,记录中的异常提示用户必须提供...

    jdk1.6与jboss5.x冲突问题的解决方法

    描述中提到的问题是尝试在后台声明一个Web服务,但在前台调用时抛出了`java.lang.UnsupportedOperationException: setProperty must be overridden by all subclasses of SOAPMessage`的异常。这通常意味着在处理...

    linux 6.x 安装elasticsearch 5.x指引

    [2016-11-06T16:27:21,712][WARN][o.e.b.JNANatives] unable to install syscall filter: java.lang.UnsupportedOperationException: seccomp unavailable: requires kernel 3.5+ with CONFIG_SECCOMP and CONFIG_...

    Java读取“桌面”、“我的文档”路径的方法

    以下是一个示例,它使用`java.lang.System`的`getProperty`方法获取用户主目录,然后添加"Documents"子目录: ```java import java.io.File; import java.util.Properties; public class Main { public static ...

    Elasticsearch-linux

    [2016-11-06T16:27:21,712][WARN][o.e.b.JNANatives]unable to install syscall filter: java.lang.UnsupportedOperationException: seccomp unavailable: requires kernel 3.5+ with CONFIG_SECCOMP and CONFIG_...

    Spring Cloud 整合Apache-SkyWalking实现链路跟踪的方法

    在某些情况下,如 JDK 11 或更高版本,可能会遇到 `java.lang.UnsupportedOperationException: Reflective setAccessible(true) disabled` 的错误。这通常是因为安全策略限制了反射操作。解决这个问题可能需要调整 ...

Global site tag (gtag.js) - Google Analytics