How does Java convert arrays to ArrayLists
This article mainly introduces how Java converts arrays into ArrayLists. It is introduced in great detail through the example code, which has a certain reference value for your study or work. Friends in need can refer to it
How to convert array to ArrayList in Java?
This paper analyzes the answer to one of the most popular questions on stack overflow. The questioner has gained a lot of reputation points, which gives him the authority to do a lot of things on stack overflow. It has nothing to do with me. Let's look at this problem first. The question is "how to convert an array into an ArrayList in Java?"
Element[] array = {new Element(1),new Element(2),new Element(3)};
1. The most popular and accepted answer
The most common and accepted answers are as follows:
ArrayList<Element> arrayList = new ArrayList<Element>(Arrays.asList(array));
First, let's look at the documentation of the construction method of ArrayList.
ArrayList (collection C): construct a list containing elements of a specific container and return it according to the order of container iterators.
Therefore, the construction method does the following:
1. Convert container C to an array
2. Copy the array to the array called "elementdata" in ArrayList
The source code of the construction method of ArrayList 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 Java util. ArrayList, but defined in Java util. A private static class in arrays. We know that the implementation of ArrayList is essentially an array, and the list returned by aslist () is a fixed size list supported by the original array. In this case, if an element in the list is added or deleted, the program will throw an exception unsupportedoperationexception.
list.add(new Element(4));
Exception in thread "main" java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be 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. What the problem shows
This question is not difficult, but it is very interesting. Every java programmer knows ArrayList, but it's easy to make such mistakes. I think that's why this problem is very hot. If it were a similar problem with a domain specific Java library, it would be far from so hot.
Many answers to this question provide the same solution, as do other stackoverflow problems. I guess when people want to answer a question, they don't care what others say.
The above is the whole content of this article. I hope it will help you in your study, and I hope you will support us a lot.