While running following code, UnsupportedOperationException
is thrown at .remove()
method.
By this code:
List list = Array.asList(array) ;
list.remove(0);
While running following code, UnsupportedOperationException
is thrown at .remove()
method.
By this code:
List list = Array.asList(array) ;
list.remove(0);
Returns a fixed-size list backed by the specified array
Thus, You can't add/remove elements to/from it.
To overcome this problem, you can do:
List modifiableList = new ArrayList(Arrays.asList(array));
If you want to remove some Object from List of objects its quite analogical way to do it directly. You need to use Iterator.
List<Integer> l = new ArrayLIst<>(); // or List<Integer> l = new ArrayLIst<Integer>();
Iterator<Integer> iter = l.iterator();
while (iter.hasNext()) {
if (iter.next().intValue() == 5) {
iter.remove();
}
}