8

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

2 Answers 2

26

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.

Sign up to request clarification or add additional context in comments.

3 Comments

It works. thanks What was wrong with my method. List<String> a=Arrays.asList(stra); The exception was not thrown in this line.
@Ashwin: That line was fine. However, the list produced by Arrays.asList() is read-only, so you can't modify it. You have to make a copy first.
okay.. with my method was the list just accessing the string[] values directly without making a copy of its own? So if any changes were allowed they would affect the String[] also right?
3

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.

6 Comments

A string is also immutable. But when you modify a string it gets allocated a new memory location and internally a new string is created. So effectively you can modify an immutable string. So I don't think here the reason is that the list returned is immutable. I think it is because when you are doing Arrays.asList() it just references the string[](does not copy the string[] and make a list).
If you have a look into the inner class call AraayList in Arrays class you will realize the fact that it cant be modify. Simply it use a private final E[] a; that is the reason you cant modify the returning list. In the case of string you cant modify a string once you created. But in string class API is developed in a manner it will handle modifications internally by creating a new object.
So you are saying that it creates a copy of the string[] but declares it as final. are you saying that it does not reference the string[] and stores a copy of its own(but as final)?
@SafeVarargs public static <T> List<T> asList(T... a) { return new ArrayList<>(a); } private final E[] a; ArrayList(E[] array) { if (array==null) throw new NullPointerException(); a = array; } I copied the code from API.
okay.. so it does create a new arraylist object. But assigns it to a final e[]. I still don't realize the reason why it is declared as final in java. Thanks for the information:)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.