Home > Java > JavaBase > body text

How to convert list to array in java?

青灯夜游
Release: 2020-11-02 15:00:33
Original
100432 people have browsed it

How to convert a list collection into an array in Java: 1. Use the parameterless toArray method, the syntax format is "Object[] toArray();"; 2. Use the toArray method that supports generics, the syntax format is "T[] toArray(T[] a);".

How to convert list to array in java?

Related recommendations: "Java Video Tutorial"

In Java, we often encounter To scenarios that require conversion between List and array. So how to convert list to array? The following article will introduce it to you.

To convert List into an array, you can use List's toArray() or toArray(T[] a) method.

Convert List to array

To convert List to array, you can call the toArray method,

There are two overloads here Method,

Generally use the second method with generic parameters:

Object[] toArray();
<T> T[] toArray(T[] a);
Copy after login

2.1. Parameterless toArray method

Object[] toArray();
Copy after login

This method will List Convert directly to Object[] array.

Java beginners like to use this method.

It is very convenient to use without parameters.

And there is no error when compiling the code.

Examples of incorrect use The code is as follows:

List<String> strList = new ArrayList<>();
strList.add("list-a");
strList.add("list-b");
String[] strArray = (String[]) strList.toArray();
Copy after login

As soon as the result is run, an error is reported directly,

cannot convert Object[] to String[]:

Exception in thread "main" java.lang.ClassCastException: 
[Ljava.lang.Object; cannot be cast to [Ljava.lang.String;
Copy after login

The correct use code is as follows:

List<String> strList = new ArrayList<>();
Object[] strArray = strList.toArray();
Copy after login

Got an Object[] which is usually useless.

2.2. Support the generic toArray method

<T> T[] toArray(T[] a);
Copy after login

This method receives an array of type T.

Note that the basic type cannot be used as a generic type parameters.

If you are using an int[] type array,

You need to replace int[] with Integer[].

The sample code used is as follows:

List<String> strList = new ArrayList<>();
strList.add("list-a");
strList.add("list-b");
String[] strArray = strList.toArray(new String[strList.size()]);
Copy after login

2.3. The array size of the toArray method input parameter

For the following code,

Analyze the relationship between the size of the initialized String array,

and the size of the List strList.size(), and the different effects

has on the return value:

List<String> strList = new ArrayList<>();
strList.add("list-a");
strList.add("list-b");
String[] strArray1 = new String[size];
String[] strArray2 = strList.toArray(strArray1);
Copy after login

2.3 .1.size < strList.size()

Now set size=0,

is less than strList.size()=2,

The code modification part is as follows:

String[] strArray1 = new String[0];
Copy after login

The returned strArray2 and strArray1 are not the same object.

2.3.2.size = strList.size()

Now set size=strList.size(),

The code modification part is as follows:

String[] strArray1 = new String[strList.size()];
Copy after login

The returned strArray2 and strArray1 are the same object.

So the following two pieces of code are equivalent.

The strArray obtained is the result we expect:

String[] strArray = strList.toArray(new String[strList.size()]);
String[] strArray = new String[strList.size()];
strList.toArray(strArray);
Copy after login

2.3.3.size > strList.size( )

Now set size=strList.size() 1,

The code modification part is as follows:

String[] strArray1 = new String[strList.size()+1];
Copy after login

The returned strArray2 and strArray1 are the same object,

But the last element of the array is null,

If there are more elements in the array than in the List,

The end of the array immediately after the list is copied is set to null,

That is, strArray1[strList.size()]=null,

This is useful for the caller to determine the real length of the array,

If you use strList.size() 2 initialization Array,

The penultimate element of the array is null.

For more programming-related knowledge, please visit: Programming Teaching! !

The above is the detailed content of How to convert list to array in java?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!