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?

user2864740
  • 54,112
  • 10
  • 112
  • 187
Ankur Agarwal
  • 19,924
  • 32
  • 117
  • 182

5 Answers5

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.

John Kugelman
  • 307,513
  • 65
  • 473
  • 519
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.

Desorder
  • 1,489
  • 12
  • 16
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.

Community
  • 1
  • 1
codebot
  • 2,203
  • 29
  • 74
  • Did you forget "String" inside <> here : `List aa = new ArrayList<>(Arrays.asList(values));` – Ankur Agarwal Sep 26 '14 at 06:06
  • 1
    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. http://stackoverflow.com/questions/4166966/what-is-the-point-of-the-diamond-operator-in-java-7 – codebot Sep 26 '14 at 06:08
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));
Ruchira Gayan Ranaweera
  • 32,406
  • 16
  • 66
  • 105
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.
  }
}
Kishan Bheemajiyani
  • 3,057
  • 3
  • 26
  • 57