在项目中采用一个枚举的集合,本人采用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 list = Arrays.asList(a[]);
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&lt;String&gt; 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的更多相关文章

  1. Java异常 —— java.lang.NoClassDefFoundError

    一直使用 Eclipse 来开发 Java . 现学习 Maven,在 cmd 下使用 Java ,出现了这样的异常:Exception in thread "main" java ...

  2. 关于Java异常java.lang.OutOfMemoryError: PermGen space

    内容来源: http://blog.csdn.net/fengyie007/article/details/1780375 PermGen space的全称是Permanent Generation ...

  3. java 异常 java.lang.OutOfMemoryError: GC overhead limit exceeded 解决

    一.异常如下: Exception in thread "main" java.lang.OutOfMemoryError: GC overhead limit exceeded ...

  4. Java 异常java.lang.IllegalArgumentException: Illegal group reference

    当字符串方法replaceAll()中替换字符含有特殊字符$如, String test = "<StreamingNo>abc</StreamingNo>" ...

  5. 常见的java异常——java.lang.IllegalStateException: Ambiguous handler methods mapped for HTTP path

    此异常是由于你的controller中有两个名字与内容相同的方法: 出现此异常时去检查你的controller中是否有重复的名字的方法:

  6. Java 异常 —— java.io.InvalidClassException: javax.xml.namespace.QName; local class incompatible

    项目中有个 WebService 接口,调试时使用 Main 方法运行,别人的机器上都能运行,就笔者的机器出问题.他们说是RP的问题…… 异常信息: java.io.InvalidClassExcep ...

  7. 异常: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 ...

  8. Hbase delete遇到的常见异常: Exception in thread "main" java.lang.UnsupportedOperationException

    hbase 执行批量删除时出现错误: Exception in thread "main" java.lang.UnsupportedOperationException at j ...

  9. Arrays.asList()后调用add,remove这些method时出现java.lang.UnsupportedOperationException异常

    String[] queryNames = request.getParameterValues("queryName"); List<String> queryNam ...

随机推荐

  1. GameMap其他初始化

    //其他初始化 init_prop();//初始化道具 init_ornamemtal();//初始化装饰物 init_monster_type_info();//初始化怪物基本信息 这个比较重要在加 ...

  2. C++代码反汇编后的堆栈寄存器EBP和ESP

    最近在分析一个进程崩溃的严重问题,其中有些过程分析需要对ebp, esp 有清晰的理解,对于ebp 和esp 相信大家都很熟悉了,但是为了使本文自成体系,我还是解释一下. ebp--栈底指针 esp- ...

  3. git安装及使用

    一.安装 1.从http://code.google.com/p/msysgit/下载Git-1.8.4-preview20130916.exe,并安装. 2.新建git目录,右键选择Git Bash ...

  4. java Hotspot 内存管理白皮书(中文翻译)

    转自: http://my.oschina.net/u/568779/blog/166891 1引言 一个健壮的 Java™2平台,Standard Edition (J2SE™)拥有一个自动内存管理 ...

  5. hdu 1271 整数对

    看了别人的解题报告a了, 大致思路就是 A=a+b*10^k+c*10^(k+1) B=a+c*10^k (在A中取出一位数后) N=A+B=2*a+b*10^k+11*c*10^k 这样就好做了,再 ...

  6. HtmlAgilityPack 总结(一)

    一个解析html的C#类库HtmlAgilityPack, HtmlAgilityPack是一个基于.Net的.第三方免费开源的微型类库,主要用于在服务器端解析html文档(在B/S结构的程序中客户端 ...

  7. Project Euler 94:Almost equilateral triangles 几乎等边的三角形

    Almost equilateral triangles It is easily proved that no equilateral triangle exists with integral l ...

  8. LR_问题_无法打开IE浏览器、监视服务器资源

    无法打开IE浏览器 使用web(http)协议录制时,无法打开IE浏览器,且生成的日志信息为 ****** Start Log Message ****** Web Recorder version ...

  9. WCF 简单示例

    WCF(Windows Communication Foundation,WCF)是基于Windows平台下开发和部署服务的软件开发包(Software Development Kit,SDK).WC ...

  10. Java API —— ArrayList类 & Vector类 & LinkList类

    1.ArrayList类     1)ArrayList类概述         · 底层数据结构是数组,查询快,增删慢         · 线程不安全,效率高     2)ArrayList案例   ...