I want to remove specific elements from my List. I don't want to do this while iterating through the list. I want to specify the value which has to be deleted. In javadocs I found the function List.remove(Object 0)
This is my code :
String str="1,2,3,4,5,6,7,8,9,10";
String[] stra=str.split(",");
List<String> a=Arrays.asList(stra);
a.remove("2");
a.remove("3");
But I get an Exception : java.lang.UnsupportedOperationException
The problem is that Arrays.asList()
returns a list that doesn't support insertion/removal (it's simply a view onto stra
).
To fix, change:
List<String> a = Arrays.asList(stra);
to:
List<String> a = new ArrayList<String>(Arrays.asList(stra));
This makes a copy of the list, allowing you to modify it.
http://docs.oracle.com/javase/6/docs/api/java/util/Arrays.html#asList%28T...%29
See this. Arrays.asList returns a fixed list. Which is an immutable one. By its definition you cant modify that object once it creates.Thats why it is throwing unsupported exception.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With