Java ProgramsCollectionsArrayList Convert to Array

ArrayList Convert to Array in Java

beginner·  Collections  ·  List

Problem

toArray() copies every element of a list into a new array, letting code that specifically needs an array work with data that started out as a List.

Given an ArrayList of strings, convert it into a plain String array.

Input
[Red, Green, Blue]
Output
Red, Green, Blue

Java Program

Java
import java.util.ArrayList; public class ArrayListConvertToArray { public static void main(String[] args) { ArrayList<String> colors = new ArrayList<>(); colors.add("Red"); colors.add("Green"); colors.add("Blue"); // Passing a typed array tells toArray() what array type to return String[] colorArray = colors.toArray(new String[0]); StringBuilder result = new StringBuilder(); for (int i = 0; i < colorArray.length; i++) { if (i > 0) result.append(", "); result.append(colorArray[i]); } System.out.println(result); } }

Output

Red, Green, Blue

Core Logic

Passing a correctly-typed, zero-length array into toArray() tells the method exactly what array type to allocate and return.

How It Works
  1. 1colors.toArray(new String[0]) asks the list for a String[] copy of its contents.
  2. 2The zero-length array passed in isn't reused directly — it just tells toArray() the target type, so it can allocate a correctly-sized array of that same type internally.
  3. 3The returned array is a completely independent copy — it holds the same element values, but changing the array afterward wouldn't affect the original list, or vice versa.
  4. 4A plain for-each loop over the resulting array prints each color, exactly as it would over the original list.
For [Red, Green, Blue], toArray(new String[0]) returns a new 3-element String[] holding the same three values in the same order.
💡

Key Point: The no-argument toArray() exists too, but it returns Object[] — passing a typed array in, as shown here, is what gets back a properly-typed String[] without needing to cast every element afterward.

Complexity
Time Complexity: O(n)Space Complexity: O(n)

Why: toArray() copies each of the list's n elements once into the new array, which itself takes space proportional to n.

Key Concepts

toArray()ArrayListtyped array

Approach 2: Java 8

Java
import java.util.ArrayList; public class ArrayListConvertToArrayStream { public static void main(String[] args) { ArrayList<String> colors = new ArrayList<>(); colors.add("Red"); colors.add("Green"); colors.add("Blue"); // Array-constructor reference lets toArray() allocate the exact right size directly String[] colorArray = colors.toArray(String[]::new); StringBuilder result = new StringBuilder(); for (int i = 0; i < colorArray.length; i++) { if (i > 0) result.append(", "); result.append(colorArray[i]); } System.out.println(result); } }

Output

Red, Green, Blue

Core Logic

Passing an array-constructor reference to toArray() lets it allocate the correctly-sized, correctly-typed array itself, instead of being handed a throwaway zero-length one.

How It Works
  1. 1colors.toArray(String[]::new) passes a method reference to String's array constructor.
  2. 2toArray() calls that reference with the list's actual size, so the array is allocated at exactly the right length in one step — no throwaway zero-length array is created first.
  3. 3The returned array is the same kind of independent, correctly-typed copy as the primary approach produces.
  4. 4A plain for-each loop over the resulting array prints each color, exactly as before.
For [Red, Green, Blue], toArray(String[]::new) returns the same 3-element String[] the new String[0] version does.
💡

Key Point: String[]::new reads as 'construct a String array of the given size' — it's the modern, slightly more efficient idiom over passing a zero-length array, since it skips allocating that throwaway array entirely.

Complexity
Time Complexity: O(n)Space Complexity: O(n)

Why: toArray() still copies each of the n elements once into the newly-allocated array, the same cost as the zero-length-array version.

Key Concepts

toArray()array constructor referenceIntFunction

Related Programs