The analysis and solution of Unsupportedoperationexception
You may encounter an exception like this when you are manipulating the list object.
Java.lang.UnsupportedOperationException
At Java.util.AbstractList.remove (Unknown Source)
At Java.util.abstractlist$itr.remove (Unknown Source)
At Java.util.AbstractCollection.remove (Unknown Source)
In a later look at the problem, I suddenly thought of a previous piece of code appears to be a problem.
That's a way.
Public list<xxxx> getxxxx () {
Return Arrays.aslist (Objectarray);
}
Where the Objectarray is an array of objects, the return is object[]. For convenience, the occasional machine
I used the Arrays.aslist () method. The result has no compilation exception. When I was running,
Throws an exception that cannot be converted. The object is roughly converted to a XXXX object conversion exception.
In fact, from the exception information can be seen that the root of the problem is that the Arrays.aslist () method returns a
Abstractlist objects, not ArrayList objects.
The anomaly has been found at all. Then let's talk about how to solve this anomaly.
My approach is to define a temporary list object in the method body, with ArrayList.
List L = new ArrayList ();
Of course, it can be paradigm-oriented.
list<xxxx> L = new arraylist<xxxx> ();
Then, add a for loop to the L list and finally return to the list.
Complete code:
Public list<xxxx> getxxxx () {
list<sysrole> list = new arraylist<sysrole> ();
for (Object O:objectarray) {
Sysrole user = (Sysrole) o;
List.add (user);
}
return list;
}
This solves the anomaly.
There is also a link in English, we can see.
http://www.theserverside.com/discussions/thread.tss?thread_id=38493