24

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.

Community
  • 1
  • 1
user3748908
  • 885
  • 2
  • 9
  • 26

3 Answers3

47

Arrays.asList() returns a list, backed by the original array. Changes you make to the list are also reflected in the array you pass in. Because you cannot add or remove elements to arrays, that is also impossible to do to lists, created this way, and that is why your remove call fails. You need a different implementation of List (ArrayList, LinkedList, etc.) if you want to be able to add and remove elements to it dynamically.

Dima
  • 39,570
  • 6
  • 44
  • 70
  • Isn't the whole point of a static safe OO language like Java not to have subclasses that don't support the superclasses methods? – Yamcha Jul 24 '17 at 19:42
  • @Yamcha yeah ... java just isn't a very well designed language. If you are looking for alternatives, I'd recommend scala. – Dima Jul 24 '17 at 20:08
  • well, I wouldn't go so far as to say Java isn't a well-designed language,but I think you could certainly argue there were some weird compromises (primitives), and other add-ons like generics that, despite many months, even years, of back and forth and design, have some shortcomings such as this example shows... – michaelok May 21 '19 at 22:11
  • @michaelok well, it's a question of taste I suppose :) If you consider a method defined on a core library interface throwing run-time exceptions to report missing implementation "well designed", then java is it! :) – Dima May 22 '19 at 11:24
  • just realized that List is an interface that is having an abstract remove method https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/List.html#remove(java.lang.Object) – Deep Roy Jan 17 '21 at 05:13
34

This is just a feature of the Arrays.asList() and has been asked before see this question

You can just wrap this in a new list

List list = new ArrayList(Arrays.asList("1",...));
Community
  • 1
  • 1
joey.enfield
  • 1,229
  • 8
  • 14
-2

Create a new list with the elements you want to remove, and then call removeAll methode.

List<Object> toRemove = new ArrayList<Object>();
for(Object a: list){
    if(true){
        toRemove.add(a);
    }
}
list.removeAll(toRemove);
Laura
  • 1,552
  • 12
  • 12
  • if downvoting, please at least provide a reason why so they know what's wrong – Ascalonian Jan 23 '15 at 14:51
  • 1
    Not the downvoter, but `removeAll()` is implemented in `AbstractCollection`, and uses `Iterator.remove()`. So, if `Iterator.remove()` doesn't work, `removeAll()` will not work. – Florent Bayle Jan 23 '15 at 15:56
  • @Ascalonian enumerating all the reasons why this is wrong would not fit into a comment :) For once, it does not work at all as FlorentBayle explained. Also, it does not (attempt to) answer the question (why the OP's code does not work). It uses things like `List` which is pointless and misleading, and bad practice. It would be a very bad (N^2) implementation if it did work. It does not make any attempt at explaining why the author thinks this needs in to be done in such an inside-out way. Etc. – Dima Jan 26 '15 at 12:31