ArrayList Convert to Array in Java
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.
Java Program
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
Core Logic
Passing a correctly-typed, zero-length array into toArray() tells the method exactly what array type to allocate and return.
- 1
colors.toArray(new String[0])asks the list for aString[]copy of its contents. - 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. - 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.
- 4A plain for-each loop over the resulting array prints each color, exactly as it would over the original list.
[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.
Why: toArray() copies each of the list's n elements once into the new array, which itself takes space proportional to n.
Key Concepts
Approach 2: Java 8
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
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.
- 1
colors.toArray(String[]::new)passes a method reference toString's array constructor. - 2
toArray()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. - 3The returned array is the same kind of independent, correctly-typed copy as the primary approach produces.
- 4A plain for-each loop over the resulting array prints each color, exactly as before.
[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.
Why: toArray() still copies each of the n elements once into the newly-allocated array, the same cost as the zero-length-array version.