Why Alibaba development manual explicitly stated Arrays.asList () method can not be used to modify its

Quotations

Alibaba java development standards when it comes to using tools Arrays.asList () method of the array into a collection, you can not use it to modify a set of related methods, its add / remove / clear method throws UnsupportedOperationException (), we look why this happens.

problem analysis

We have to be tested

Why Alibaba development manual explicitly stated Arrays.asList () method can not be used to modify its


Annotated can unlock three lines are comments, after running the statute does appear in the said exception. We look at Arrays.asList () to do what action.

Why Alibaba development manual explicitly stated Arrays.asList () method can not be used to modify its


It appears to be a normal way, but in fact you click through to the ArrayList found, in fact, is not what we usually use ArrayList ArrayList.

Why Alibaba development manual explicitly stated Arrays.asList () method can not be used to modify its


But an inner class Arrays inside. And this internal class does not add, clear, remove method, so exceptions thrown from the fact AbstractList.

Why Alibaba development manual explicitly stated Arrays.asList () method can not be used to modify its


Point into place you will find to throw an exception, clear bottom layer is also called to remove it will also throw an exception.

to sum up

1.Arrays.asList () not to use, but it is still the underlying array;

2. If a Arrays.asList (), it is best not to use its method of operation set;

3.List list = new ArrayList <> (Arrays.asList ( "a", "b", "c")) may be coated with a layer of such real ArrayList (array to a set There are many ways out, reference may be https: / /stackoverflow.com/questions/157944/create-arraylist-from-array).


Guess you like

Origin blog.51cto.com/14480698/2437677