How to convert list to array in java?
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);".
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);
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<String> 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<String> strList = new ArrayList<>(); Object[] strArray = strList.toArray();
Got an Object[] which is usually useless.
2.2. Support the generic toArray method
<T> 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<String> 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<String> 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 < strList.size()
Now set size=0,
is less than strList.size()=2,
The code modification part is as follows:
String[] strArray1 = new String[0];
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()];
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);
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];
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Guide to Random Number Generator in Java. Here we discuss Functions in Java with examples and two different Generators with ther examples.

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4
