Monday, October 11, 2010

java.lang.UnsupportedOperationException on java.util.AbstractList

java.lang.UnsupportedOperationException: null
at java.util.AbstractList.remove(AbstractList.java:144) ~[na:1.6.0_04]
at java.util.AbstractList$Itr.remove(AbstractList.java:360) ~[na:1.6.0_04]
at java.util.AbstractCollection.remove(AbstractCollection.java:252) ~[na:1.6.0_04]

Arrays.asList(Object[]) returns type Arrays$ArrayList is of (extends) type AbstractList.

Arrays$ArrayList does not override remove(int) so the method is called on the superclass AbstractList

new LinkedList(Arrays.asList(String[] something));

Use any List implementation that supports a modifyable result. In the above, LinkedList works because, instantiates a new modifiable list using the immutible list created from Arrays.aslist(...).

No comments: