Using an enumeration collection in the project, I use an empty collection collections.emptylist () in collections to add an exception:
Common collections are as follows:
Private list<vacationcategory> Vacationcategorys = Collections.emptylist ();
Reported errors are as follows:
--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)
..............................
The JDK API is interpreted as follows:
Java.lang.CloneNotSupportedException
Cloning exceptions are not supported. When the Cloneable interface is not implemented or the Clone method is not supported, the exception is thrown by calling its clone () method.
Check online for the reason that the partial collection type is the same but is missing some methods or not supported.
If the special case is as follows:
(1) ARRAYS.ASLISVT () is often used to call add,remove these method when the Java.lang.UnsupportedOperationException exception occurs. This is due to:
ARRAYS.ASLISVT () returns java.util.arrays$arraylist instead of ArrayList. Arrays$arraylist and ArrayList are inherited Abstractlist,remove,add etc. method in Abstractlist is the default throw Unsupportedoperationexception and does not do anything. ArrayList override these method to manipulate the list, but Arrays$arraylist does not override remove (int), add (int), and so on, so the throw Unsupportedoperationexception.
The workaround is to use iterator, or convert to ArrayList
List List = Arrays.aslist (a[]);
List arrayList = new arrayList (list);
(2)
Private list<vacationcategory> Vacationcategorys = Collections.emptylist ();
When executing a method such as Remove,add, throw this exception, I will change the above code to:
Private list<vacationcategory> Vacationcategorys = new arraylist<vacationcategory> ();
Without this error, I looked at the source code:
The source code is as follows:
This class is in the class of 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-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 has 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 This collection unexpectedly does not have the corresponding add,remove and so on method
Java Exception java.lang.UnsupportedOperationException