博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java.lang.UnsupportedOperationException
阅读量:6932 次
发布时间:2019-06-27

本文共 2380 字,大约阅读时间需要 7 分钟。

hot3.png

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

 

http://blog.csdn.net/thunderous/article/details/3693362

转载于:https://my.oschina.net/xiaominmin/blog/1596990

你可能感兴趣的文章
Python练习2
查看>>
js-yaml简单使用
查看>>
Java基础学习总结(1)——equals方法
查看>>
Java认证:J2ME中用低级界面实现数字输入
查看>>
Java注释模板
查看>>
Linux实用工具
查看>>
Spring3 MVC详解
查看>>
Docker学习总结(2)——Docker实战之入门以及Dockerfile(二)
查看>>
网络研讨会的邀请:网络公开课_请搭载我们的雷霆战机进行跨平台的数据迁移...
查看>>
Windows Server 2012之部署Windows Server 更新服务(3)
查看>>
实易科技荣获“2012年度中国行业信息化最佳产品奖”
查看>>
Mybatis应用学习——简单使用示例
查看>>
linux进程控制
查看>>
数据结构与算法笔记(八)
查看>>
vim中集成DevHelp帮助
查看>>
js正则表达式学习-小记
查看>>
cocos2dx 导出c++类供lua使用
查看>>
Android 之LayerDrawable层叠样式layer-list及自定义颜色ProgressBar
查看>>
llinux企业常用服务---samba共享
查看>>
puppet自动化管理
查看>>