java.lang.UnsupportedOperationException
2016-03-10 16:05
603 查看
在使用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
例子:
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
相关文章推荐
- java class修改
- 深入理解Java虚拟机笔记---方法调用
- Java 代理使用及代理原理
- java 协程 实现 Akka
- java 远程通讯之 rmi
- Java远程通讯可选技术及原理
- Java中参数传递是值传递,还是引用传递
- spring 注解 extends
- JAVA并发编程实践笔记
- java 汉字按照拼音排序
- java Gearman helloword
- java.lang.OutOfMemoryError:GC overhead limit exceeded
- Java URL协议扩展实现
- java报表中AIX字体丢失解决方案
- java 之方法调用 方法传参 值传递还是引用传递字节码
- Java中的10颗语法糖
- Java集合框架之LinkedList-----用LinkedList模拟队列和堆栈
- jdk 版本并发 性能
- compareTo()方法返回值 String java
- java 对象池