0
0
JavaHow-ToBeginner · 2 min read

Java How to Convert ArrayList to Array Easily

Use arrayList.toArray(new Type[arrayList.size()]) to convert an ArrayList to an array in Java, for example: String[] arr = list.toArray(new String[list.size()]).
📋

Examples

Input["apple", "banana", "cherry"]
Output["apple", "banana", "cherry"]
Input[]
Output[]
Input["one"]
Output["one"]
🧠

How to Think About It

To convert an ArrayList to an array, you create a new array of the same type and size, then use the ArrayList's toArray method to fill it. This method copies all elements from the list into the new array.
📐

Algorithm

1
Create an ArrayList with elements.
2
Create a new array of the same type and size as the ArrayList.
3
Call the ArrayList's <code>toArray</code> method with the new array as argument.
4
The method fills the array with the list's elements and returns it.
5
Use or print the resulting array.
💻

Code

java
import java.util.ArrayList;
import java.util.Arrays;

public class ConvertArrayList {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("apple");
        list.add("banana");
        list.add("cherry");

        String[] array = list.toArray(new String[list.size()]);
        System.out.println(Arrays.toString(array));
    }
}
Output
[apple, banana, cherry]
🔍

Dry Run

Let's trace converting ["apple", "banana", "cherry"] from ArrayList to array.

1

Create ArrayList

list = ["apple", "banana", "cherry"]

2

Create new array

new String[3] -> [null, null, null]

3

Call toArray

array = list.toArray(new String[3]) -> ["apple", "banana", "cherry"]

StepArray Contents
Before toArray[null, null, null]
After toArray["apple", "banana", "cherry"]
💡

Why This Works

Step 1: Why create a new array?

The toArray method needs an array to fill, so you create one with the same size and type as the ArrayList.

Step 2: How does toArray work?

It copies each element from the ArrayList into the new array in order.

Step 3: Resulting array

The returned array contains all elements from the list and can be used like any normal array.

🔄

Alternative Approaches

Using toArray() without argument
java
import java.util.ArrayList;
import java.util.Arrays;

public class ConvertArrayListAlt {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("apple");
        list.add("banana");
        list.add("cherry");

        Object[] array = list.toArray();
        System.out.println(Arrays.toString(array));
    }
}
Returns an Object array, so you may need to cast elements later.
Using Java 8 Streams
java
import java.util.ArrayList;
import java.util.List;

public class ConvertArrayListStream {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("apple");
        list.add("banana");
        list.add("cherry");

        String[] array = list.stream().toArray(String[]::new);
        for (String s : array) {
            System.out.print(s + " ");
        }
    }
}
Uses streams to convert, more flexible but slightly more complex.

Complexity: O(n) time, O(n) space

Time Complexity

The method copies each element once, so time grows linearly with the number of elements.

Space Complexity

A new array of the same size is created, so space usage is proportional to the list size.

Which Approach is Fastest?

Using toArray(new Type[size]) is efficient and type-safe; streams add overhead but offer flexibility.

ApproachTimeSpaceBest For
toArray(new Type[size])O(n)O(n)Simple, type-safe conversion
toArray()O(n)O(n)Quick but returns Object[] needing casting
Streams toArrayO(n)O(n)Flexible, functional style
💡
Always pass a new array of the correct type and size to toArray for type safety.
⚠️
Beginners often forget to pass a typed array to toArray, resulting in an Object array instead of the desired type.