Java ArrayList toArray() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
ArrayList

Definition

In Java, toArray() copies the elements of an ArrayList into a new Object[], in list order. toArray(T[] a) copies them into an array of a specific type, such as String[], so you do not need casts afterwards.

The array is a copy: changing an array slot does not change the list, and the other way round. Arrays print as a code like [Ljava.lang.String;@..., so use Arrays.toString(arr) to see the contents.

An instance method of java.util.ArrayList (Java 1.2+): a resizable-array list with index access that allows null and duplicates and is not thread-safe.

Syntax

The typed form is the one you usually want.

Java
List<String> list = new ArrayList<>(Arrays.asList("a", "b"));
Object[] objs = list.toArray();                  // Object[]
String[] arr = list.toArray(new String[0]);      // String[]
String[] arr2 = list.toArray(String[]::new);     // Java 11+
CallWhat it does
list.toArray()A new Object[] with all elements.
list.toArray(new String[0])A new String[] of the right size.
list.toArray(String[]::new)The same, with the Java 11 generator overload.
Arrays.toString(arr)Prints the array contents.

Passing new String[0] is the common idiom: since the empty array is too small, toArray creates a new array of the correct type and size.

Library declarations
Java
// java.util.ArrayList<E>
public Object[] toArray()
public <T> T[] toArray(T[] a)

// java.util.Collection<E> (default method, Java 11+)
default <T> T[] toArray(IntFunction<T[]> generator)

Parameters

ParameterDescription
a (T[])Only in the typed form. Used directly if it is big enough; otherwise only its type is used to create a new array.

Return Value

Return ValueDescription
Object[] or T[]An array holding all elements in list order.

Compare with Similar Methods

Copying data out of a list:

MethodHow it differs
clone()Copies into another ArrayList.
subList(a, b)A view of a range, still a list.
Arrays.asList(arr)The opposite direction: a fixed-size list backed by an array.
forEach(action)Processes elements without copying them.

Java Examples

Example 1 — Convert a List to a String Array

Copy the fruits into a String[], then change the array.

Java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ArrayListToArrayDemo {
    public static void main(String[] args) {
        List<String> fruits = new ArrayList<>(Arrays.asList("kiwi", "mango", "plum"));

        String[] arr = fruits.toArray(new String[0]);
        System.out.println(arr.length + " " + Arrays.toString(arr));

        arr[0] = "lime";
        System.out.println("array: " + Arrays.toString(arr));
        System.out.println("list:  " + fruits);
    }
}

How It Works

  1. Right type. The empty new String[0] tells toArray to build a String[], sized for all three fruits.
  2. Readable print. Arrays.toString shows the array contents in brackets, just like a list prints.
  3. Edit the copy. arr[0] = "lime" changes only the array slot.
  4. List untouched. The list still starts with kiwi, because the array is an independent copy.

Example 2 — Object[] vs a Typed Array

Compare the no-argument form with the typed form when summing numbers.

Java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ArrayListToArrayTypes {
    public static void main(String[] args) {
        List<Integer> nums = new ArrayList<>(Arrays.asList(4, 5, 6));

        Object[] objs = nums.toArray();
        System.out.println("Object[] element type: " + objs.getClass().getComponentType().getSimpleName());

        Integer[] boxed = nums.toArray(new Integer[0]);
        int sum = 0;
        for (int n : boxed) {
            sum += n;
        }
        System.out.println("Integer[] sum: " + sum);

        int second = (Integer) objs[1];
        System.out.println("second via Object[]: " + second);
    }
}

How It Works

  1. Plain form. toArray() always returns an Object[]; its component type is Object, not Integer.
  2. Typed form. toArray(new Integer[0]) returns an Integer[], so the loop can unbox each element straight into an int.
  3. Sum. 4 + 5 + 6 = 15.
  4. Casting each element. Reading from the Object[] needs a cast per element, (Integer) objs[1], which is why the typed form is usually nicer.

Example 3 — What Happens with an Exact or Larger Array

Pass an array that fits exactly, then one that is too big.

Java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ArrayListToArraySized {
    public static void main(String[] args) {
        List<String> codes = new ArrayList<>(Arrays.asList("A1", "B2"));

        String[] exact = new String[2];
        String[] result = codes.toArray(exact);
        System.out.println("same array returned? " + (result == exact));

        String[] big = {"x", "x", "x", "x"};
        codes.toArray(big);
        System.out.println("bigger array: " + Arrays.toString(big));
    }
}

How It Works

  1. Exact fit. The two-slot array is big enough, so toArray fills it and returns that same array instead of making a new one.
  2. Too big. The four-slot array is also filled in place, starting at index 0.
  3. null marker. The slot right after the last element (index 2) is set to null, marking where the list's data ends.
  4. Rest untouched. Index 3 still holds the old "x". Passing new String[0] avoids these leftovers.

Frequently Asked Questions

Use list.toArray(new String[0]) (with your element type), or list.toArray(String[]::new) on Java 11 and later.
Because of type erasure the list does not know its element type at run time. Pass a typed array to get a String[].
Both work. new String[0] is the common, simplest choice and is usually just as fast.
No. toArray copies the elements, so the array and the list are independent.
Use Arrays.toString(array). Printing the array directly shows a type code and a hash, not the elements.

Next: trimToSize()

Shrink the capacity to the current size.

trimToSize() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful