java 异常java.lang.UnsupportedOperationException
在项目中采用一个枚举的集合,本人采用Collections中的空集合Collections.emptyList()在添加时发生异常:
常见集合如下:
private List<VacationCategory> vacationcategorys = Collections.emptyList();
报错误如下:
-- Encapsulated exception ------------\
java.lang.UnsupportedOperationException
at java.util.AbstractList.add(AbstractList.java:131)
at java.util.AbstractList.add(AbstractList.java:91)
at
com.unutrip.callcenter.vacation.web.condition.VacationOrderConditionConvertor.setProductStyle(VacationOrderConditionConvertor.java:155)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
..............................
JDK API解释如下:
java.lang.CloneNotSupportedException
不支持克隆异常。当没有实现Cloneable接口或者不支持克隆方法时,调用其clone()方法则抛出该异常。
在网上查一下原因是因为部分集合类型一样但是缺少部分方法或不支持。
如特殊情况如下:
(1)常常使用Arrays.asLisvt()后调用add,remove这些method时出现java.lang.UnsupportedOperationException异常。这是由于:
Arrays.asLisvt()
返回java.util.Arrays$ArrayList,
而不是ArrayList。Arrays$ArrayList和ArrayList都是继承AbstractList,remove,add等
method在AbstractList中是默认throw
UnsupportedOperationException而且不作任何操作。ArrayList
override这些method来对list进行操作,但是Arrays$ArrayList没有override
remove(int),add(int)等,所以throw UnsupportedOperationException。
解决方法是使用Iterator,或者转换为ArrayList
List arrayList = new ArrayList(list);
(2)
private List<VacationCategory> vacationcategorys = Collections.emptyList();
执行remove,add等method时,抛出此异常,本人将上述代码改为:
private List<VacationCategory> vacationcategorys = new ArrayList<VacationCategory>();
没有此错误,于是我查看一下源代码:
源码如下:
此类在Collections的类中:
/**
* The empty list (immutable). This list is serializable.
*
* @see #emptyList()
*/
public static final List EMPTY_LIST = new EmptyList();
/**
* Returns the empty list (immutable). This list is serializable.
*
* <p>This example illustrates the type-safe way to obtain an empty list:
* <pre>
* List<String> s = Collections.emptyList();
* </pre>
* Implementation note: Implementations of this method need not
* create a separate <tt>List</tt> object for each call. Using this
* method is likely to have comparable cost to using the like-named
* field. (Unlike this method, the field does not provide type safety.)
*
* @see #EMPTY_LIST
* @since 1.5
*/
public static final <T> List<T> emptyList() {
return (List<T>) EMPTY_LIST;
}
/**
* @serial include
*/
private static class EmptyList
extends AbstractList<Object>
implements RandomAccess, Serializable {
// use serialVersionUID from JDK 1.2.2 for interoperability
private static final long serialVersionUID = 8842843931221139166L;
public int size() {return 0;}
public boolean contains(Object obj) {return false;}
public Object get(int index) {
throw new IndexOutOfBoundsException("Index: "+index);
}
// Preserves singleton property
private Object readResolve() {
return EMPTY_LIST;
}
}
EmptyList此集合竟然没有相应的add,remove等方法
java 异常java.lang.UnsupportedOperationException的更多相关文章
- Java异常 —— java.lang.NoClassDefFoundError
一直使用 Eclipse 来开发 Java . 现学习 Maven,在 cmd 下使用 Java ,出现了这样的异常:Exception in thread "main" java ...
- 关于Java异常java.lang.OutOfMemoryError: PermGen space
内容来源: http://blog.csdn.net/fengyie007/article/details/1780375 PermGen space的全称是Permanent Generation ...
- java 异常 java.lang.OutOfMemoryError: GC overhead limit exceeded 解决
一.异常如下: Exception in thread "main" java.lang.OutOfMemoryError: GC overhead limit exceeded ...
- Java 异常java.lang.IllegalArgumentException: Illegal group reference
当字符串方法replaceAll()中替换字符含有特殊字符$如, String test = "<StreamingNo>abc</StreamingNo>" ...
- 常见的java异常——java.lang.IllegalStateException: Ambiguous handler methods mapped for HTTP path
此异常是由于你的controller中有两个名字与内容相同的方法: 出现此异常时去检查你的controller中是否有重复的名字的方法:
- Java 异常 —— java.io.InvalidClassException: javax.xml.namespace.QName; local class incompatible
项目中有个 WebService 接口,调试时使用 Main 方法运行,别人的机器上都能运行,就笔者的机器出问题.他们说是RP的问题…… 异常信息: java.io.InvalidClassExcep ...
- 异常:java.lang.UnsupportedOperationException: Manual close is not allowed over a Spring managed SqlSession
使用mybatis-3.2.2.jar + mybatis-spring-1.2.0.jar集成时,报以下异常: 15:42:48.538 [Thread-1] DEBUG o.s.b.f.s.Dis ...
- Hbase delete遇到的常见异常: Exception in thread "main" java.lang.UnsupportedOperationException
hbase 执行批量删除时出现错误: Exception in thread "main" java.lang.UnsupportedOperationException at j ...
- Arrays.asList()后调用add,remove这些method时出现java.lang.UnsupportedOperationException异常
String[] queryNames = request.getParameterValues("queryName"); List<String> queryNam ...
随机推荐
- spring @resource @ Autowired
Spring中什么时候用@Resource,什么时候用@service 当你需要定义某个类为一个bean,则在这个类的类名前一行使用@Service("XXX"),就相当于讲这个类 ...
- 浏览器后退按钮导致jquery动态添加的select option值丢失的解决方法
监控浏览器返回功能 判断浏览器返回功能 禁用浏览器的后退按钮 JS禁止浏览器后退键 http://volunteer521.iteye.com/blog/830522/ 浏览器返回功能 判断上一页面来 ...
- Jmeter 快速入门教程(三-1) --添加响应断言(即loadrunner中所指的检查点)
[版权所有: whoistester.com & jmeter.cf] 上一节课,我们创建了一个测试场景,并进行了少量vuser的负载测试. 有时候我们执行了测试,但是发现并不是所有事务都执行 ...
- 将集成spring的项目从tomcat上移植到weblogic下存在的问题
当在weblogic下部署时, 1.需要jersey-servlet-xx.jar,jersey-core-xx.jar,jersey-server-xx.jar: 2.在web.xml中全局参数co ...
- lintcode :nth to Last Node In List 链表倒数第n个节点
题目: 链表倒数第n个节点 找到单链表倒数第n个节点,保证链表中节点的最少数量为n. 样例 给出链表 3->2->1->5->null和n = 2,返回倒数第二个节点的值1. ...
- switch… case 语句的用法
switch… case 语句的用法 public class Test7 { public static void main(String[] args) { int i=5; switch(i ...
- ibatis框架文件配置
最近2天在学ibatis,心里也有一些心得,就把它写下来了. 首先是配置一下ibatis的环境,添加ibatis2.X.jar,mysql-connection-bin.5.1.8.jar,建立一个w ...
- GitHub最全的前端资源汇总仓库(包括前端学习、开发资源、求职面试等)
在GitHub上收集的最全的前端资源汇总(包括前端学习.前端开发资源.前端求职面试等) 个人结合github上各位大神分享的资源进行了简单的汇总整理,每一个条目下面都有丰富的资料,是前端学习.工作的好 ...
- VC程序查错之内存访问异常
作者:langouster 先来看下面这张图,相信很多程序员都见过类似. ---------------------------test1.exe - 应用程序错误------------------ ...
- Android:ViewPager制作幻灯片
布局: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:androi ...