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 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等方法


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM