[Java] Convert ArrayList to array

How to convert ArrayList to array and notes

ListToArray.java


//ArrayList generation
ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("A");
arrayList.add("B");
arrayList.add("C");

String[] array = (String[])arrayList.toArray();

System.out.println("ArrayList = " + arrayList.toString());
System.out.println("Array= " + Arrays.toString(array));

Result (NG)

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

why

Try to get the class to return

ListToArray.java


//ArrayList generation
//Omission
System.out.println(arrayList.toArray().getClass());

result

class [Ljava.lang.Object;

An Object type array is returned. .. ..

Fix

ListToArray.java


//ArrayList generation
//Omission
//0 for the number of elements in the array or arrayList.size()Put in
String[] array = arrayList.toArray(new String[arrayList.size()]);
//String[] array = arrayList.toArray(new String[0]);* Processing will be slower (described later)

System.out.println("ArrayList = " + arrayList.toString());
System.out.println("Array= " + Arrays.toString(array));

Result (OK)

ArrayList = [A, B, C]
Array= [A, B, C]

Try deep digging

java:java.util.ArrayList.class



public <T> T[] toArray(T[] a) {
   if (a.length < size)
     return (T[]) Arrays.copyOf(elementData, size, a.getClass());
     System.arraycopy(elementData, 0, a, 0, size);
     if (a.length > size)
         a[size] = null;
     return a;
}

System.arraycopy(elementData, 0, a, 0, size); I was copying String [] as it was. If it is ~~ new String [0], it will enter Arrays.copyOf (3rd line) and copy the elements one by one, which will slow down the process. ~~ Updated on 2020/04/29

I tried to verify the processing time [Java] ArrayList → Should I specify the size for array conversion

By the way

Only the type specified in the conversion source ArrayList can be specified in the toArray argument. If you have a different class, you will get a compile error.

Recommended Posts

[Java] Convert ArrayList to array
[Java] Convert array to ArrayList * Caution
Convert Java Powerpoint to XPS
How to convert Java radix
How to initialize Java array
[Java] array
Java array
Java array
java (array)
Java array
[Java] Array
Java array
How to convert a file to a byte array in Java
java array
[Java] Array
Convert alphabet to 26 base + array length
Java basic learning content 2 (array / ArrayList)
[Java] Convert 1-to-N List to Map
[Java] How to use List [ArrayList]
[Android] Convert Android Java code to Kotlin
[Java] Conversion from array to List
How to make a Java array
Convert Swift 2D array to C 2D array
Convert a Java byte array to a string in hexadecimal notation
For Java beginners: List, Map, Iterator / Array ... How to convert?
Convert 2D array to csv format with Java 8 Stream API
java array variable
Convert all Android apps (Java) to Kotlin
Convert from java UTC time to JST time
[Java] Convert Object type null to String type
Convert SVG files to PNG files in Java
[Java] Introduction to Java
Convert an array of strings to numbers
[Easy-to-understand explanation! ] How to use ArrayList [Java]
[Java] From two Lists to one array list
[Java] Convert DB code to code value using enum
Convert a string to a character-by-character array with swift
How to convert a solidity contract to a Java contract class
Convert Java nested beans to aaa.bbb [0] .ccc format
Two-dimensional array by just starting to study Java
[Java] How to convert one element of a String type array to an Int type
[Java] Convert PDF version
Changes from Java 8 to Java 11
Sum from Java_1 to 100
About Java Array List
Kotlin's improvements to Java
[Java ~ Array ~] Study memo 4
Introduction to java command
[Java] What is ArrayList?
Launch Docker from Java to convert Office documents to PDF
<java> Read Zip file and convert directly to string
Mastering Kotlin ~ Convert Java File to Kotlin File Road to Graduation ~ Part 3
Convert Java enum enums and JSON to and from Jackson
Sample code to convert List to List <String> in Java Stream
[Java] Convert character strings to uppercase / lowercase (AOJ⑨-swap uppercase and lowercase)
Mastering Kotlin ~ Convert Java File to Kotlin File Road to Graduation ~ Part 2
Java applications convert Word (DOC / DOCX) documents to PDF
Mastering Kotlin ~ Convert Java File to Kotlin File Road to Graduation ~ Part 1
How to convert A to a and a to A using AND and OR in Java
[Java] Convert JSON to Java and Java to JSON-How to use GSON and Jackson-
Gzip-compress byte array in Java and output to file