How to know if List.remove() is "Unsupported"?
Asked Answered
L

5

5

I have this:

import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
public class ListTest {

    public static void main(String[] args) {


        String[] values = { "yes", "no"};
        List<String> aa = Arrays.asList(values);
        System.out.println(aa.getClass().getName());
        aa.remove(0);
    }

}

It gives:

$ java ListTest 
java.util.Arrays$ArrayList
Exception in thread "main" java.lang.UnsupportedOperationException
    at java.util.AbstractList.remove(AbstractList.java:161)
    at ListTest.main(ListTest.java:12)

Question: I understand why I am getting this exception. It is because the ArrayList class from inside Arrays.java is being used which does not have a remove() method. My question is how can someone(any user, like me) know before using that the List they received that it does not contain a remove method?

Logrolling answered 26/9, 2014 at 5:45 Comment(0)
I
8

There is no way to know. All of the List<T> methods that change the list are listed as optional. A subclass can implement them or not. Unfortunately the API does not include a method like isReadOnly(), so there's no way to check if these methods will throw exceptions without calling them.

It is the responsibility of the owner of the read-only list not to pass it to methods that will try to change it.

Illyrian answered 26/9, 2014 at 5:50 Comment(0)
J
1

As long as the Interface is properly implemented, writing a remove method that throws an Exception is completely legal. So, no you won't know until it breaks... That is when Javadoc comes handy.

Jook answered 26/9, 2014 at 5:53 Comment(0)
N
1

Here is the solution

import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
class ListTest {
    public static void main(String[] args) {
        String[] values = { "yes", "no"};
        List<String> aa = new ArrayList<>(Arrays.asList(values));
        System.out.println(aa.getClass().getName());
        aa.remove(0);
  }
}

and here is a very good explanation for your problem.

Hope this helps.

Ned answered 26/9, 2014 at 6:1 Comment(2)
Did you forget "String" inside <> here : List<String> aa = new ArrayList<>(Arrays.asList(values));Logrolling
No no. That is diamond inferance. Allows only in Java 7 and higher. You can use it as both ways if you java version is higher than 7. Check this out. #4167466Ned
G
0

Arrays.asList returns a List wrapper for an array. This wrapper has a fixed size and is directly backed by the array hence there can't be modify methods. So better to keep in mind that else i am not sure how to know it because at least compile time you will not get an error .

If you want to get a Collection from a array to modify you can use

Collection c = new ArrayList(Arrays.asList(values));
Glindaglinka answered 26/9, 2014 at 5:51 Comment(1)
The OP knows this. The question was whether or not there was any way to detect this lack of implementation without knowing the concrete type of the list.Vocoid
T
0

Well what u have done that is directly return like this. ex. array in easy way something like this String array[] means its returning array now tell me is there anyway to remove that element from that i mean from the array? that is belongs from list only

one possible way is .

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

class ListTest {
    public static void main(String[] args) {
        String[] values = { "yes", "no"};
        List<String> aa = new ArrayList<>(Arrays.asList(values)); // this will return arraylist (according that u wanted).
        System.out.println(aa.getClass().getName());
        aa.remove(0); // removes element as of list interface have remove method.
  }
}
Trilingual answered 29/9, 2014 at 5:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.