1

How can I remove an Element from a List in Java? This is, what I tried:

import java.util.Arrays;
import java.util.List;

public class Test{

    public static void main(String[] args) {

        String words = "monday|tuesday|wednesday|thursday|friday|saturday|sunday";

        List<String> list = Arrays.asList(words.split("\\|"));

        System.out.println(list.toString());

        list.remove(1);

        System.out.println(list.toString());
    }
}

And I want it to print [monday, tuesday, wednesday, thursday, friday, saturday, sunday]
and [monday, wednesday, thursday, friday, saturday, sunday] after that.

But that doesn't really work...
It prints the first thing but instead of printing the new List after that, it gives me this error in Eclipse:

Exception in thread "main" java.lang.UnsupportedOperationException
    at java.util.AbstractList.remove(Unknown Source)
    at Test.main(Test.java:14)
James Dunn
  • 8,064
  • 13
  • 53
  • 87
  • You need to use a different List, one that supports the remove method. – Hovercraft Full Of Eels Dec 13 '17 at 17:34
  • In the future, consider [searching the site](https://www.google.com/search?q=site%3Astackoverflow.com+java+UnsupportedOperationException+list+remove) before asking (please click on the link). A similar question has been asked hundreds to thousands of times on this site. – Hovercraft Full Of Eels Dec 13 '17 at 17:35
  • Ok. I'm sorry. I should have looked for it before! –  Dec 13 '17 at 17:37

0 Answers0