Java.lang.UnsupportedOperationException when calling List.remove (index)
Possible duplicate:
Why am I getting an UnsupportedOperationException when trying to remove from the list?
When I call List.remove (index) or list.remove (element) it throws a java.lang.UnsupportedOperationException. The only relevant error code is:
17:08:10 [SEVERE] at java.util.AbstractList.remove(Unknown Source)
Here's an example:
String line = "cmd /say This is a test";
String[] segments = line.split(" ");
String cmd = segments[0];
List rest = Arrays.asList(segments);
rest.remove(0); // This line raises the exception
Does anyone know why this is happening? In my activation code, I have checked and there is an item at index 0 that needs to be removed.
+3
source to share
1 answer
From Arrays.asList()
JavaDoc:
Returns a fixed-size list supported by the specified array. (Changes to the returned "write through" list to an array.)
So instead of a fixed size list:
List rest = Arrays.asList(segments);
create a new list of variables:
List<String> rest = new ArrayList<String>(Arrays.asList(segments));
+12
source to share