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
🌐
GeeksforGeeks
geeksforgeeks.org › java › arraylist-array-conversion-java-toarray-methods
ArrayList to Array Conversion in Java : toArray() Methods - GeeksforGeeks
July 23, 2025 - This is a manual method of copying all the ArrayList elements to the String Array[]. // Returns the element at the specified index in the list. public E get(int index) ... // Java program to convert a ArrayList to an array // using get() in a loop.
🌐
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); } } }
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);
🌐
BeginnersBook
beginnersbook.com › 2013 › 12 › how-to-convert-array-to-arraylist-in-java
How to convert an array to ArrayList in java
In the end of the program, we are printing the elements of the ArrayList, which displays 6 elements, four elements that were added to arraylist from array and 2 new elements that are added using add() method. import java.util.*; public class JavaExample { public static void main(String[] args) { // Array declaration and initialization String cityNames[]={"Agra", "Mysore", "Chandigarh", "Bhopal"}; // Array to ArrayList conversion ArrayList<String> cityList= new ArrayList<String>(Arrays.asList(cityNames)); // Adding new elements to the list after conversion cityList.add("Chennai"); cityList.add("Delhi"); //print ArrayList elements using advanced for loop for (String str: cityList) { System.out.println(str); } } }
🌐
Scaler
scaler.com › home › topics › arraylist to array java
Convert ArrayList to Array in Java - Scaler Topics
January 12, 2024 - A trivial way of converting an ArrayList to an Array is by declaring an array of the given size and keeping adding the numbers sequentially. But, is this the only way? ... As discussed earlier, we'll declare an array of the given size, then ...
🌐
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.
🌐
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.
Find elsewhere
🌐
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.
🌐
Reddit
reddit.com › r/javahelp › is arraylist to array possible?
r/javahelp on Reddit: Is ArrayList to Array possible?
February 23, 2022 -

Hello, I am looking to copy all the contents of my integer ArrayList to a completely new int array so it can be returned and displayed on the console.

My method return type is int[] so I am trying to figure out how to copy my ArrayList to a new array, I feel this is something very simple so I might be overthinking but any help would be beneficial.

Top answer
1 of 3
5
You really could have Googled this https://www.geeksforgeeks.org/arraylist-array-conversion-java-toarray-methods/
2 of 3
1
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
🌐
GeeksforGeeks
geeksforgeeks.org › java › arraylist-toarray-method-in-java-with-examples
ArrayList toArray() method in Java with Examples - GeeksforGeeks
Then, the toArray() method is called ... how to convert an ArrayList of integers into an array of the integers using the toArray(T[]) method....
Published   December 19, 2018
🌐
CodeAhoy
codeahoy.com › java › How-To-Convery-ArrayList-To-Array
ArrayList to Array Conversion in Java | CodeAhoy
March 1, 2020 - What’s with the weird-looking argument new Integer[0]? The reason it is there because the type of returned array is determined using this argument. In other words, the toArray(...) method uses the type of the argument, Integer to create another array of the same type, places all elements from ArrayList into the array in order and returns it.
🌐
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 the string array languages.toArray(arr); System.out.print("Array: "); for(String item:arr) { System.out.print(item+", "); } } }
🌐
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) ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › array-to-arraylist-conversion-in-java
Array to ArrayList Conversion in Java - GeeksforGeeks
July 11, 2025 - This approach involves creating a new ArrayList and using the add() method to insert each element from the array. ... // Java program to illustrate conversion // of an array to an ArrayList import java.util.ArrayList; public class Geeks { public ...
🌐
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 - Hi! In today's lesson, we'll talk about How To Initialize An Array In Java and How Convert Array to an Arraylist. Arrays are an extension of the Container class in Java
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › ArrayList.html
ArrayList (Java Platform SE 8 )
October 20, 2025 - Java™ Platform Standard Ed. 8 ... public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, Serializable · Resizable-array implementation of the List interface. Implements all optional list operations, and permits all elements, including null. In addition to ...