How to find out if List.remove () is "unsupported"?

I have it:

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); } } 

He 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 get this exception. This is because the ArrayList class is used from within Arrays.java , which does not have a remove() method. My question is, how does someone (any user, like me), before using, what List got that it does not contain the remove method?

+5
source share
5 answers

Unable to find out. All 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 such as isReadOnly() , so there is no way to check whether these methods throw exceptions without calling them.

The responsibility of the owner of a read-only list is to pass it on to methods that will try to change it.

+8
source

As long as the interface is properly implemented, writing a delete method that throws an exception is completely legal. So, no, you won’t know until it breaks ... That's when Javadok comes in handy.

+1
source

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 of your problem.

Hope this helps.

+1
source

Arrays.asList returns a List wrapper for array . This shell has a fixed size and is directly supported by array , so change methods cannot be changed. So it’s better to keep in mind that I’m still not sure how to know this, because at least the compilation time you won’t get an error.

If you want to get a collection from an array for modification, you can use

 Collection c = new ArrayList(Arrays.asList(values)); 
0
source

Well, what you did is a direct return. ex. an array in its simplest form, something like this array String [] means that its returned array now tells me that there is after all to remove this element from what I mean from the array? which belongs only from the list

one of the possible ways is possible.

 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. } } 
0
source

Source: https://habr.com/ru/post/1203456/


All Articles