在项目中采用一个枚举的集合,本人采用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. JavaScript高级---组合模式设计

    一.设计模式 javascript里面给我们提供了很多种设计模式: 工厂.桥.组合.门面.适配器.装饰者.享元.代理.观察者.命令.责任链 在前面我们实现了工厂模式和桥模式 工厂模式 : 核心:为了生 ...

  2. jquery-validation 学习总结

    一.用前必备 项目主页:http://bassistance.de/jquery-plugins/jquery-plugin-validation/ API: http://jquery.bassis ...

  3. 拼写sql语句随笔

    DECLARE @str NVARCHAR(1000)='SELECT * FROM dbo.V_MicroUserInfo ',@where VARCHAR(1000)=' where 1=1' D ...

  4. hdu 2065 "红色病毒"问题

    指数型母函数的应用 求A B C D 在规定条件下n个元素的排列个数,先写出指数型母函数 G(X) = ( 1 + x + x^2/2! + x^3/3! +... )^2 * ( 1+ x^2/2! ...

  5. cojs 疯狂的魔法树 疯狂的颜色序列 题解报告

    疯狂的魔法树 一个各种操作大杂烩的鬼畜数据结构题目 首先我们注意到树的形态是半随机的 我们可以树分块,对树分成若干个块 对于每个块我们维护一个add标记表示增量 维护一个vis标记表示覆盖量 注意标记 ...

  6. [GCJ]Password Attacker

    https://code.google.com/codejam/contest/4214486/dashboard#s=p0 排列组合.DP递推式,如下代码.dp[m][n]表示长度为n的字符串里有m ...

  7. redhat 新装后不能联网

    1.ifconfig查看是否有ip地址,如果没有就配置,命令如下: [root@redhat ~]# system-config-network 设置DHCP 为 [*] [ok]后,重新ifconf ...

  8. apache&nginx资料汇总

    http://liudaoru.iteye.com/blog/336338 aquid:http://os.51cto.com/art/201009/225813.htm 数据库各种讲座:http:/ ...

  9. ctrlsf插件

    输入CtrlSF pattern <dir>来搜索 想要搜索当前文件,用CtrlSF pattern %p(或者C-R %) 在结果界面,p键预览,回车进入闭关内关闭搜索结果框,t键打开一 ...

  10. JSTL、EL、ONGL、Struts标签的区别与使用

     一.JSTL 来源 我们使用JSP开发信息展现非常方便,也可嵌入java代码用来实现相关逻辑,但同样带来了很多问题: jsp维护难度增加 出事提示不明确,不容易提示 分工不明确等 解决上面的问题可以 ...