`
tansitongba
  • 浏览: 482152 次
文章分类
社区版块
存档分类
最新评论

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。

例子:

Java代码 收藏代码
  1. packagecom.test;
  2. importjava.util.Arrays;
  3. importjava.util.List;
  4. publicclassTestUnsupported{
  5. publicstaticvoidmain(String[]args){
  6. String[]s={
  7. "one","two","three","four","five",
  8. "six","seven","eight","nine","ten",
  9. };
  10. Lista=Arrays.asList(s);
  11. System.out.println(
  12. "a.contains("+s[0]+")="+
  13. a.contains(s[0]));
  14. a.add("eleven");//Unsupported
  15. a.remove(s[0]);//Unsupported
  16. }
  17. }

运行后,抛出异常如下:

Java代码 收藏代码
  1. Exceptioninthread"main"java.lang.UnsupportedOperationException
  2. atjava.util.AbstractList.add(AbstractList.java:151)
  3. atjava.util.AbstractList.add(AbstractList.java:89)
  4. atcom.test.TestUnsupported.main(TestUnsupported.java:28)

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

Java代码 收藏代码
  1. ListarrayList=newArrayList(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://lzrzhao.iteye.com/blog/466860

分享到:
评论

相关推荐

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

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

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

    主要介绍了Android 兼容性问题:java.lang.UnsupportedOperationException解决办法的相关资料,需要的朋友可以参考下

    Android webveiw 出现栈错误解决办法

    主要介绍了Android webveiw 出现栈错误解决办法的相关资料,出现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系统安装教程

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

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

    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以上的版本

    java 面试题 总结

    java.lang.String类是final类型的,因此不可以继承这个类、不能修改这个类。为了提高效率节省空间,我们应该用StringBuffer类 3、int 和 Integer 有什么区别 Java 提供两种不同的类型:引用类型和原始类型(或内置...

    JAVA面试题最全集

    一、Java基础知识 1.Java有那些基本数据类型,String是不是基本数据类型,他们有何区别。 2.字符串的操作: 写一个方法,实现字符串的反转,如:输入abc,输出cba 写一个方法,实现字符串的替换,如:输入...

    超级有影响力霸气的Java面试题大全文档

     java.lang.String类是final类型的,因此不可以继承这个类、不能修改这个类。为了提高效率节省空间,我们应该用StringBuffer类 6、int 和 Integer 有什么区别  Java 提供两种不同的类型:引用类型和原始类型(或...

Global site tag (gtag.js) - Google Analytics