How do I convert an array to ArrayList in Java?

Source: Internet
Author: User

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?"

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<ElementArrays.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));
in"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

array = {new Element(1new Element(2)};Listlistnew ArrayList<element>(array.length);Collections.addAll(listarray);
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?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.