Translate from: How to Convert arrays to ArrayList in Java?
This article analyzes the answer to one of the hottest questions on stack overflow, where the questioner gets a lot of prestige points, allowing him to get the right to do a lot of things on the stack overflow. It has nothing to do with me, let's take a look at it first.
The question is "How do I convert an array to ArrayList in Java?"
Element[] Array = {new Element (1), new Element (2), new Element (3)};
1. The most popular answer is also accepted by the most people
The most commonly accepted answer is the following:
arraylist<element> ArrayList = new Arraylist<element> (arrays.aslist (array));
First, let's look at the documentation for ArrayList's construction method.
ArrayList (Collection <? extends E > C): Constructs a list of elements that contain a specific container, and is returned according to the order of the container iterators.
So the construction method does the following things:
1. Converting container C to an array
2. Copy the array into an array called "Elementdata" in ArrayList
The source code for the ArrayList construction method is as follows:
Public ArrayList (collection<? extends e> c) { elementdata = C.toarray (); size = Elementdata.length; if (elementdata.getclass () = object[].class) elementdata = arrays.copyof (elementdata, size, object[].class);}
2. Another popular answer
Another popular answer is:
list<element> list = arrays.aslist (array);
This is not the best because the size of the list returned by Aslist () is fixed. In fact, the returned list is not a java.util.ArrayList, but a private static class defined in java.util.Arrays . We know that the implementation of ArrayList is essentially an array, whereas the list returned by Aslist () is a fixed-size list supported by the original array. In this case, if you add or remove elements from the list, the program throws an exception unsupportedoperationexception.
List.add (New Element (4));
Exception in thread "main" Java.lang.classcastexception:java.util.arrays$arraylist cannot is cast to Java.util.ArrayList at Collection. Convertarray.main (convertarray.java:22)
3. Another solution
This solution is provided by Otto
Element[] Array = {new Element (1), new Element (2)}; list<element> list = new arraylist<element> (array.length); Collections.addall (list, array);
4. The issue of the indication of something
The problem is not difficult, but it is very interesting. Every Java programmer knows ArrayList, but it's easy to make such a mistake. I think this is the reason why the problem is very hot. If it's a similar problem for a particular domain of Java libraries, it's far less fiery.
There are many answers to this question that provide the same solution, as is the case with other problems with StackOverflow. I guess when people want to answer a question, they don't care what they say.
Reference: links to issues on StackOverflow
Translate from: How to Convert arrays to ArrayList in Java?
How do I convert an array to ArrayList in Java?