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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Linux new version
SublimeText3 Linux latest version

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver Mac version
Visual web development tools
