In Java, how to convert an array ArrayList? In Java how to convert an array ArrayList?

 

turn:

liushaobo

In Java, how to convert an array to ArrayList?

Translated from: How to Convert For Array ArrayList in to the Java?

This paper analyzes the answers on Stack Overflow most popular of a problem, the questioner received a lot of prestige points that he got to do a lot of things on Stack Overflow rights. That's fine with me, let's first look at this issue now.
The question is "how to convert an array in Java as ArrayList?"

1
Element[] array = { new Element( 1 ), new Element( 2 ), new Element( 3 )};

 

1. The most popular is the most widely accepted answer

The most common and most people are below the accepted answer:

1
ArrayList<Element> arrayList = new ArrayList<Element>(Arrays.asList(array));

 

First, we look at the document constructor ArrayList under.
ArrayList (Collection <? Extends E> c): a structure comprising a list of specific elements of the container, the container according to the order and returns an iterator.
So what constructor does the following:
1. Convert the container c is an array
2. Array copied to ArrayList called "elementData" in the array
of source ArrayList construction method is as follows:

1
2
3
4
5
6
7
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:

1
List<Element> list = Arrays.asList(array);

 

This is not the best, because asList () Returns the size of the list is fixed. In fact, the list is not returned java.util.ArrayList , but is defined in java.util.Arrays a private static class. We know that ArrayList on realization of nature is an array, and asList () returned list is supported by the original array of fixed-size list. In this case, if you add or delete elements in the list, the program will throw an exception UnsupportedOperationException .

1
list.add( new Element( 4 ));
1
2
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. Also a solution

The solution provided by Otto

1
2
3
Element[] array = { new Element( 1 ), new Element( 2 )};
List<element> list = new ArrayList<element>(array.length);
Collections.addAll(list, array);

 

4. indicate what the problem

This problem is not difficult, but very interesting. Every Java programmer knows ArrayList, but it is easy to commit such a mistake. I think this is the cause of this problem is the fire of it. If it is a specific problem areas like Java library, it is not so far from the fiery.

There are many answers to this question provides the same solution to other issues of StackOverflow as well. I guess when people want to answer a question, not what other people say.

Reference: problem link on StackOverflow

Translated from: How to Convert For Array ArrayList in to the Java?

Translated from: How to Convert For Array ArrayList in to the Java?

This paper analyzes the answers on Stack Overflow most popular of a problem, the questioner received a lot of prestige points that he got to do a lot of things on Stack Overflow rights. That's fine with me, let's first look at this issue now.
The question is "how to convert an array in Java as ArrayList?"

1
Element[] array = { new Element( 1 ), new Element( 2 ), new Element( 3 )};

 

1. The most popular is the most widely accepted answer

The most common and most people are below the accepted answer:

1
ArrayList<Element> arrayList = new ArrayList<Element>(Arrays.asList(array));

 

First, we look at the document constructor ArrayList under.
ArrayList (Collection <? Extends E> c): a structure comprising a list of specific elements of the container, the container according to the order and returns an iterator.
So what constructor does the following:
1. Convert the container c is an array
2. Array copied to ArrayList called "elementData" in the array
of source ArrayList construction method is as follows:

1
2
3
4
5
6
7
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:

1
List<Element> list = Arrays.asList(array);

 

This is not the best, because asList () Returns the size of the list is fixed. In fact, the list is not returned java.util.ArrayList , but is defined in java.util.Arrays a private static class. We know that ArrayList on realization of nature is an array, and asList () returned list is supported by the original array of fixed-size list. In this case, if you add or delete elements in the list, the program will throw an exception UnsupportedOperationException .

1
list.add( new Element( 4 ));
1
2
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. Also a solution

The solution provided by Otto

1
2
3
Element[] array = { new Element( 1 ), new Element( 2 )};
List<element> list = new ArrayList<element>(array.length);
Collections.addAll(list, array);

 

4. indicate what the problem

This problem is not difficult, but very interesting. Every Java programmer knows ArrayList, but it is easy to commit such a mistake. I think this is the cause of this problem is the fire of it. If it is a specific problem areas like Java library, it is not so far from the fiery.

There are many answers to this question provides the same solution to other issues of StackOverflow as well. I guess when people want to answer a question, not what other people say.

Reference: problem link on StackOverflow

Translated from: How to Convert For Array ArrayList in to the Java?

Guess you like

Origin www.cnblogs.com/libin6505/p/11027211.html