I'm trying to remove some elements from a List
, but even the simplest examples, as the ones in this answer or this, won't work.
public static void main(String[] args)
{
List<String> list = Arrays.asList("1", "2", "3", "4");
for (Iterator<String> iter = list.listIterator(); iter.hasNext();)
{
String a = iter.next();
if (true)
{
iter.remove();
}
}
}
Exception in thread "main" java.lang.UnsupportedOperationException
at java.util.AbstractList.remove(Unknown Source)
at java.util.AbstractList$Itr.remove(Unknown Source)
Using a normal Iterator
instead of a ListIterator
doesn't help.
What am I missing? I'm using java 7.