Convert an array into a list. We usually get used to writing it like this: List <string> List = arrays. aslist ("1", "2 ");
So we get a list, but the implementation class of this list is Java. util. arrays. arraylist (instead of Java. util. arraylist ).
By analyzing the JDK source code, we can find that Java. util. arrays. arraylist (which is converted to list) inherits the java. util. abstractlist class.
Let's take a look at what the java. util. abstractlist class looks like? We can find that
Public e set (INT index, e element)
Public e set (INT index, e element)
Public E remove (INT index)
Public void add (INT index, e element) | public Boolean add (e) call add (INT index, e element)
All of the above methods throw an unsupportedoperationexception.
Therefore, the list converted from arrays. aslist is actually a javasactlist, which can be accessed like a list, but cannot be modified.
This also explains why the add and remove operations on the list output from arrays. aslist throw the unsupportedoperationexception. From the JDK code perspective, this is the cause.
In other words, Java. util. arrays. arraylist is actually just a decoration of the array. We can see the implementation in it. e get (INT index), e set (INT index, e element) and other methods are all Array Operations, his goal is to provide the ability to access arrays just like the access list. In essence, it is still an array.
Arrays. aslist usage and exceptions