Either:

Foo[] array = list.toArray(new Foo[0]);

or:

Foo[] array = new Foo[list.size()];
list.toArray(array); // fill the array

Note that this works only for arrays of reference types. For arrays of primitive types, use the traditional way:

List<Integer> list = ...;
int[] array = new int[list.size()];
for(int i = 0; i < list.size(); i++) array[i] = list.get(i);

Update:

It is recommended now to use list.toArray(new Foo[0]);, not list.toArray(new Foo[list.size()]);.

From JetBrains Intellij Idea inspection:

There are two styles to convert a collection to an array: either using a pre-sized array (like c.toArray(new String[c.size()])) or using an empty array (like c.toArray(new String[0]).

In older Java versions using pre-sized array was recommended, as the reflection call which is necessary to create an array of proper size was quite slow. However since late updates of OpenJDK 6 this call was intrinsified, making the performance of the empty array version the same and sometimes even better, compared to the pre-sized version. Also passing pre-sized array is dangerous for a concurrent or synchronized collection as a data race is possible between the size and toArray call which may result in extra nulls at the end of the array, if the collection was concurrently shrunk during the operation.

This inspection allows to follow the uniform style: either using an empty array (which is recommended in modern Java) or using a pre-sized array (which might be faster in older Java versions or non-HotSpot based JVMs).

Answer from Eng.Fouad on Stack Overflow
🌐
W3Schools
w3schools.com › java › ref_arraylist_toarray.asp
Java ArrayList toArray() Method
T refers to the data type of items in the list. Specify the return type of toArray(): import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<String> cars = new ArrayList<String>(); cars.add("Volvo"); cars.add("BMW"); cars.add("Ford"); cars.add("Mazda"); String[] carsArray = new String[4]; carsArray = cars.toArray(carsArray); for(String item : carsArray) { System.out.println(item); } } } Try it Yourself » ·
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › List.html
List (Java Platform SE 8 )
October 20, 2025 - If the list fits in the specified ... (This is useful in determining the length of the list only if the caller knows that the list does not contain any null elements.) Like the toArray() method, this method acts as bridge between array-based and collection-based API...
Top answer
1 of 11
1513

Either:

Foo[] array = list.toArray(new Foo[0]);

or:

Foo[] array = new Foo[list.size()];
list.toArray(array); // fill the array

Note that this works only for arrays of reference types. For arrays of primitive types, use the traditional way:

List<Integer> list = ...;
int[] array = new int[list.size()];
for(int i = 0; i < list.size(); i++) array[i] = list.get(i);

Update:

It is recommended now to use list.toArray(new Foo[0]);, not list.toArray(new Foo[list.size()]);.

From JetBrains Intellij Idea inspection:

There are two styles to convert a collection to an array: either using a pre-sized array (like c.toArray(new String[c.size()])) or using an empty array (like c.toArray(new String[0]).

In older Java versions using pre-sized array was recommended, as the reflection call which is necessary to create an array of proper size was quite slow. However since late updates of OpenJDK 6 this call was intrinsified, making the performance of the empty array version the same and sometimes even better, compared to the pre-sized version. Also passing pre-sized array is dangerous for a concurrent or synchronized collection as a data race is possible between the size and toArray call which may result in extra nulls at the end of the array, if the collection was concurrently shrunk during the operation.

This inspection allows to follow the uniform style: either using an empty array (which is recommended in modern Java) or using a pre-sized array (which might be faster in older Java versions or non-HotSpot based JVMs).

2 of 11
408

An alternative in Java 8:

String[] strings = list.stream().toArray(String[]::new);

Since Java 11:

String[] strings = list.toArray(String[]::new);
🌐
Baeldung
baeldung.com › home › java › java array › collection.toarray(new t[0]) or .toarray(new t[size])
Collection.toArray(new T[0]) or .toArray(new T[size]) - Java
January 8, 2024 - Next, let’s put together a JMH (Java Microbenchmark Harness) benchmark for our trials. We’ll configure the size and type parameters of the collection for the benchmark: @Param({ "10", "10000", "10000000" }) private int size; @Param({ "array-list", "tree-set" }) private String type; Additionally, we’ll define benchmark methods for the zero-sized and the pre-sized toArray variants: @Benchmark public String[] zero_sized() { return collection.toArray(new String[0]); } @Benchmark public String[] pre_sized() { return collection.toArray(new String[collection.size()]); } Running the above benchmark on an 8 vCPU, 32 GB RAM, Linux x86_64 Virtual Machine with JMH (v1.28) and JDK (1.8.0_292) furnishes the results shown below.
🌐
GeeksforGeeks
geeksforgeeks.org › java › arraylist-toarray-method-in-java-with-examples
ArrayList toArray() method in Java with Examples - GeeksforGeeks
toArray(T[] a) -> a is the array into which list elements are stored; if it is not large enough, a new array of the same type is returned ... The following Java program demonstrates how to convert an ArrayList to an array using the toArray() method.
Published   January 19, 2026
🌐
Codecademy
codecademy.com › docs › java › arraylist › .toarray()
Java | ArrayList | .toArray() | Codecademy
January 5, 2024 - The .toArray() method of the ArrayList class is a common method in Java that converts an ArrayList into an array and returns the newly created array. The returned array contains all the elements in the ArrayList in the correct order.
Find elsewhere
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › ArrayList.html
ArrayList (Java Platform SE 8 )
October 20, 2025 - The size, isEmpty, get, set, iterator, and listIterator operations run in constant time. The add operation runs in amortized constant time, that is, adding n elements requires O(n) time. All of the other operations run in linear time (roughly speaking).
🌐
CodeGym
codegym.cc › java blog › java collections › convert list to array in java
Convert List to Array in Java
December 10, 2024 - We’ll be using Class ArrayList ... a toArray() method which directly converts the contents of any list into an array while retaining the placement of text in the Array as it was in the original list....
🌐
Reddit
reddit.com › r/learnjava › convert list into an array in java
r/learnjava on Reddit: Convert List into an array in Java
July 17, 2020 -

Suppose we have a List<Employee> object and we want to convert it into Employee[ ].

List<Employee> list=new ArrayList<>();

We can convert list to object type array in following ways:

Employee[] empArray = list.toArray(new Employee[0]);

or

Employee[] empArray = new Employee[list.size()];

list.toArray(empArray);

But to convert arrays to primitive types. you have to convert it into following way:-

List<Integer> list = ...;

int[] array = new int[list.size()];

for(int i = 0; i < list.size(); i++) array[i] = list.get(i);

Is there any other way to convert List to its specific type of array?

🌐
Coderanch
coderanch.com › t › 730788 › java › List-toArray
List & toArray() (Java in General forum at Coderanch)
Hi, I am reading Jeanne's OCP 1Z0-809 Oracle Certified Professional Java SE 8 Programmer II book. I ran into a problem on Chapter 3, page 105. I tested the code shown in the book. But it gives me ClassCastException. Is down casting allow here? String[] array2 = (String[]) list.toArray(); This returns an Object array.
🌐
Quora
quora.com › What-are-the-different-ways-I-can-convert-an-ArrayList-to-an-Array-on-Java
What are the different ways I can convert an ArrayList to an Array on Java? - Quora
Answer (1 of 3): If the type of the element was String, for example, and if you had an ArrayList list. Then to extract a String[] efficiently, you would use: String[] strings = list.toArray(new String[list.size()]); I know it’s a drag having to yourself create the String array, but the ot...
🌐
Vultr
docs.vultr.com › java › standard-library › java › util › ArrayList › toArray
Java ArrayList toArray() - Convert To Array | Vultr Docs
September 27, 2024 - The toArray() method in Java's ArrayList class is essential for converting an ArrayList to a standard array.
🌐
GeeksforGeeks
geeksforgeeks.org › java › convert-list-to-array-in-java
Convert List to Array in Java - GeeksforGeeks
July 11, 2025 - Space Complexity: O(n), where n is the size of the list. The toArray() method without any arguments returns an array containing all of the elements in the list in the proper sequence .
🌐
Educative
educative.io › answers › how-to-convert-a-java-list-to-an-array
How to convert a Java list to an array
import java.util.ArrayList; public class ListToArray { public static void main(String[] args) { // A list of size 4 which is to be converted: ArrayList<Integer> list = new ArrayList<>(); list.add(1); list.add(2); list.add(3); list.add(4); // ArrayList converted to Object[] array: Object[] objArr = list.toArray(); for(Object obj: objArr){ // Using casting before performing addition: System.out.println((Integer)obj + 1); } } } Run ·
🌐
Scaler
scaler.com › home › topics › convert list to array in java
Convert List to Array in Java - Scaler Topics
April 14, 2023 - The toArray() method is a popular method used to convert list to array in java.
🌐
Scaler
scaler.com › home › topics › toarray() in java
toArray() in Java - Scaler Topics
May 4, 2023 - This function either takes in no parameter as per the first syntax or takes in an array of Type T in which the element of the list will be stored. The toArray() method returns an Object array if no parameter is passed. Otherwise, if an array is passed as an argument, it returns a new array of Type T i.e.
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › converting a list to an array in java
Converting a List to an Array in Java - A Comprehensive Guide
3 weeks ago - The toArray() method is provided by the List interface itself and simplifies the process of converting a List to an array. It returns an array containing all the elements of the List.
🌐
LambdaTest Community
community.lambdatest.com › general discussions
Correct Way to Convert ArrayList to Array in Java - LambdaTest Community
January 12, 2025 - What is the correct way to convert an ArrayList to an Array in Java? I have a List and need to populate an array with its values. Check the code below: ArrayList tiendas; List tiendasList; tiendas = new ArrayList (); Resources res = this.getBaseContext().getResources(); XMLParser saxparser = new XMLParser(marca, res); tiendasList = saxparser.parse(marca, res); tiendas = tiendasList.toArray(); this.adaptador = new adaptadorMarca(this, R.layout.fil...
Top answer
1 of 8
65

If you look at the implementation of toArray(T[] a) of ArrayList<E> class, it is like:

public <T> T[] toArray(T[] a) {
    if (a.length < size)
        // Make a new array of a's runtime type, but my contents:
        return (T[]) Arrays.copyOf(elementData, size, a.getClass());
    System.arraycopy(elementData, 0, a, 0, size);
    if (a.length > size)
        a[size] = null;
    return a;
}

Problem with this method is that you need to pass array of the same generic type. Now consider if this method do not take any argument then the implementation would be something similar to:

public <T> T[] toArray() {
    T[] t = new T[size]; // compilation error
    return Arrays.copyOf(elementData, size, t.getClass());
}

But the problem here is that you can not create generic arrays in Java because compiler does not know exactly what T represents. In other words creation of array of a non-reifiable type (JLS §4.7) is not allowed in Java.

Another important quote from Array Store Exception (JLS §10.5):

If the component type of an array were not reifiable (§4.7), the Java Virtual Machine could not perform the store check described in the preceding paragraph. This is why an array creation expression with a non-reifiable element type is forbidden (§15.10.1).

That is why Java has provided overloaded version toArray(T[] a).

I will override the toArray() method to tell it that it will return an array of E.

So instead of overriding toArray(), you should use toArray(T[] a).

Cannot Create Instances of Type Parameters from Java Doc might also be interesting for you.

2 of 8
21

Generic information is erased at runtime. JVM does not know whether your list is List<String> or List<Integer> (at runtime T in List<T> is resolved as Object), so the only possible array type is Object[].

You can use toArray(T[] array) though - in this case JVM can use the class of a given array, you can see it in the ArrayList implementation:

public <T> T[] toArray(T[] a) {
    if (a.length < size)
        // Make a new array of a's runtime type, but my contents:
        return (T[]) Arrays.copyOf(elementData, size, a.getClass());
🌐
Board Infinity
boardinfinity.com › blog › how-to-convert-arraylist-to-array-in-java
Convert ArrayList to Array in Java | Board Infinity
January 3, 2025 - Uses toArray() With the help of a Generator function. Uses functional programming as one of its factors. Concise and modern syntax. The library is very compatible with Java’s functional programming model. For developers who already know and want to use the conventional way to implement for loop or enhanced for loop to copy elements from the ArrayList to the array. import java.util.ArrayList; public class ArrayListToArrayExample { public static void main(String[] args) { ArrayList<String> list = new ArrayList<>(); list.add("Apple"); list.add("Banana"); list.add("Cherry"); // Convert ArrayList to Array String[] array = new String[list.size()]; for (int i = 0; i < list.size(); i++) { array[i] = list.get(i); } // Print Array for (String s : array) { System.out.println(s); } } }