java.lang.UnsupportedOperationException at java.util.AbstractList.remove(Unknown Source)
Easy work around is just to pass in the List into an ArrayList
's constructor.
For example:
String valuesInArray[]={"1","2","3","4"}; List modifiableList = new ArrayList(Arrays.asList(valuesInArray)); System.out.println(modifiableList.remove("1") + " remove flag"); System.out.println(" collcetion "+ modifiableList);
Response:
true remove flag
collcetion [2, 3, 4]
Slight correction: no, it's not an unmodifiable Collection. It just doesn't support adding and removing elements, because it is backed by the supplied array and arrays aren't resizeable. But it supports operations like list.set(index, element)
I was having this problem, because I was also initializing my list with Arrays.asList
:
List<String> names = Arrays.asList("a", "b", "c");
To solve the problem, I used addAll
instead:
List<String> names = new ArrayList<String>();
names.addAll(Arrays.asList("a", "b", "c"));
This way you can edit the list, add new items or remove.