3

Possible Duplicate:
Why I get UnsupportedOperationException when trying to remove from the List?

When I call List.remove(index) or list.remove(element) it raises a java.lang.UnsupportedOperationException. The only relevent error code is this:

17:08:10 [SEVERE]       at java.util.AbstractList.remove(Unknown Source)

Here is 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 have any idea why this is happening? In my actuall code, I checked and there is a element at index 0 to be removed.

Community
  • 1
  • 1
Tom
  • 846
  • 5
  • 18
  • 30

1 Answers1

14

From Arrays.asList()'s JavaDoc:

Returns a fixed-size list backed by the specified array. (Changes to the returned list "write through" to the array.)

So instead of the fixed-size list:

List rest = Arrays.asList(segments);

create a new variable-size list:

List<String> rest = new ArrayList<String>(Arrays.asList(segments));
Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417