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 โ€บ java_arraylist.asp
Java ArrayList
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Server Java Syllabus Java Study Plan Java Interview Q&A Java Certificate ... An ArrayList is like a resizable array. It is part of the java.util package and implements the List interface. The difference between a built-in array and an ArrayList in Java, is that the size of an array cannot be modified (if you want to add or remove elements to/from an array, you have to create a new one).
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ arraylist-array-conversion-java-toarray-methods
ArrayList to Array Conversion in Java : toArray() Methods - GeeksforGeeks
July 23, 2025 - ... // A Java program to demonstrate ... GFG { public static void main(String[] args) { List<Integer> al = new ArrayList<Integer>(); al.add(10); al.add(20); al.add(30); al.add(40); // Error: incompatible types: Object[] // cannot ...
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ ref_arraylist_toarray.asp
Java ArrayList toArray() Method
If the array in the argument is large enough to contain all of the list items then this method will return the argument itself after writing the list items into it. ... T refers to the data type of items in the list. ... 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); } } }
๐ŸŒ
Codecademy
codecademy.com โ€บ docs โ€บ java โ€บ arraylist โ€บ .toarray()
Java | ArrayList | .toArray() | Codecademy
January 5, 2024 - Beginner Friendly.Beginner Friendly17 ... conversion. In the example below, the .toArray() method is used to convert an ArrayList named fruitsList containing strings to an array of strings....
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);
๐ŸŒ
How to do in Java
howtodoinjava.com โ€บ home โ€บ collections framework โ€บ java arraylist โ€บ java arraylist.toarray()
Java ArrayList.toArray() with Examples - HowToDoInJava
January 12, 2023 - ArrayList<String> list = new ArrayList<>(); list.add("A"); list.add("B"); list.add("C"); list.add("D"); //Convert to object array Object[] array = list.toArray(); //Iterate and convert to desired type for(Object o : array) { String s = (String) ...
๐ŸŒ
CodeAhoy
codeahoy.com โ€บ java โ€บ How-To-Convery-ArrayList-To-Array
ArrayList to Array Conversion in Java | CodeAhoy
March 1, 2020 - ... package com.codeahoy.ex; import ... elements to the list list.add(5); list.add(10); list.add(15); // Convert ArrayList to Array Integer[] array = list.toArray(new Integer[0]); // Print the array for (Integer n : array) { System.out.println(n); } } }...
๐ŸŒ
Programiz
programiz.com โ€บ java-programming โ€บ examples โ€บ convert-list-array
Java Program to Convert a List to Array and Vice Versa
languages = new ArrayList<>(); // Add elements in the list languages.add("Java"); languages.add("Python"); languages.add("JavaScript"); System.out.println("ArrayList: " + languages); // Create a new array of String type String[] arr = new String[languages.size()]; // Convert ArrayList into ...
Find elsewhere
๐ŸŒ
Blogger
javarevisited.blogspot.com โ€บ 2011 โ€บ 06 โ€บ converting-array-to-arraylist-in-java.html
3 Ways to Convert an Array to ArrayList in Java? Example
Anyway itโ€™s simpler than you ... ArrayList to array. In this example of ArrayList to array we will convert our assetTradingList into String array....
๐ŸŒ
Board Infinity
boardinfinity.com โ€บ blog โ€บ how-to-convert-arraylist-to-array-in-java
Convert ArrayList to Array in Java | Board Infinity
January 3, 2025 - This tutorial will teach you how to convert an ArrayList to an Array in Java using different methods and code examples.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ arraylist-toarray-method-in-java-with-examples
ArrayList toArray() method in Java with Examples - GeeksforGeeks
The following Java program demonstrates how to convert an ArrayList to an array using the toArray() method. This example showcases the basic usage of the ArrayList and how its elements can be retrieved and stored in the array.
Published ย  December 19, 2018
๐ŸŒ
BeginnersBook
beginnersbook.com โ€บ 2013 โ€บ 12 โ€บ how-to-convert-array-to-arraylist-in-java
How to convert an array to ArrayList in java
In the last tutorial, you learned how to convert an ArrayList to Array in Java. In this guide, you will learn how to convert an array to ArrayList. Syntax: ArrayList<T> arraylist= new ArrayList<T>(Arrays.asList(arrayname)); Example: In this example, we are using Arrays.asList() method to convert ...
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ arraylist to array java
Convert ArrayList to Array in Java - Scaler Topics
January 12, 2024 - Java provides mechanisms like ... way of converting an ArrayList to an Array is by declaring an array of the given size and keeping adding the numbers sequentially....
๐ŸŒ
Vultr
docs.vultr.com โ€บ java โ€บ standard-library โ€บ java โ€บ util โ€บ ArrayList โ€บ toArray
Java ArrayList toArray() - Convert To Array | Vultr Docs
September 27, 2024 - This functionality is crucial when you need to pass the ArrayList data to APIs or libraries that only accept arrays or when performing operations that require array data structures. In this article, you will learn how to use the toArray() method in various scenarios, exploring both its basic form and its more advanced form that makes use of generics. Dive into practical code examples that demonstrate how to effectively convert an ArrayList into both Object arrays and specifically typed arrays.
๐ŸŒ
CodeGym
codegym.cc โ€บ java blog โ€บ java collections โ€บ arraylist to array conversion in java
How to Convert an Array to ArrayList in Java
January 15, 2025 - ArrayList<Integer> boltsInventory = new ArrayList<Integer>(): Collections.addAll(boltsInventory, bolts); This will pass the entire contents of the Array bolts to the new ArrayList.
๐ŸŒ
BeginnersBook
beginnersbook.com โ€บ 2013 โ€บ 12 โ€บ how-to-convert-arraylist-to-string-array-in-java
How to convert ArrayList to string array in java
2. Run a for loop from 0 till the arraylist size and at every iteration get arraylist element using get() method and assign it to the array arr. 3. Once all elements are added to the array, print array elements using enhanced for loop. import java.util.*; public class JavaExample { public static ...
๐ŸŒ
Codecademy
codecademy.com โ€บ learn โ€บ learn-java โ€บ modules โ€บ learn-java-arrays-and-arraylists โ€บ cheatsheet
Learn Java: Arrays and ArrayLists Cheatsheet | Codecademy
In Java, an array is used to store a list of elements of the same datatype. Arrays are fixed in size and their elements are ordered. ... Using the {} notation, by adding each element all at once. Using the new keyword, and assigning each position of the array individually. ... To change an element value, select the element via its index and use the assignment operator to set a new value. ... An ArrayList can easily be modified using built in methods.