T[] toArray (T[] a);"."/> T[] toArray (T[] a);".">

Home>Article>Java> How to convert list to array in java?

How to convert list to array in java?

青灯夜游
青灯夜游 Original
2020-11-02 15:00:33 100243browse

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[] toArray(T[] a);

2.1. Parameterless toArray method

Object[] toArray();

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 strList = new ArrayList<>(); strList.add("list-a"); strList.add("list-b"); String[] strArray = (String[]) strList.toArray();

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;

The correct use code is as follows:

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

Got an Object[] which is usually useless.

2.2. Support the generic toArray method

 T[] toArray(T[] a);

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 strList = new ArrayList<>(); strList.add("list-a"); strList.add("list-b"); String[] strArray = strList.toArray(new String[strList.size()]);

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 strList = new ArrayList<>(); strList.add("list-a"); strList.add("list-b"); String[] strArray1 = new String[size]; String[] strArray2 = strList.toArray(strArray1);

2.3 .1.size e705c6c420abdc9ad0f569fab860dfab strList.size( )

Now set size=strList.size() 1,

The code modification part is as follows:

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

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!

Statement:
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