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
Top answer
1 of 11
1518

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
409

An alternative in Java 8:

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

Since Java 11:

String[] strings = list.toArray(String[]::new);
🌐
GeeksforGeeks
geeksforgeeks.org › java › arraylist-array-conversion-java-toarray-methods
ArrayList to Array Conversion in Java : toArray() Methods - GeeksforGeeks
July 11, 2026 - It is specified by toArray in interface Collection and interface List ... It returns an array containing all of the elements in this list in the correct order. ... import java.io.*; import java.util.List; import java.util.ArrayList; class GFG ...
Discussions

java - Create ArrayList from array - Stack Overflow
Note that the returned type for ... is NOT java.util.ArrayList. It's an inner type, which emulates an ArrayList but actually directly references the passed array and makes it "write through" (modifications are reflected in the array). It forbids modifications through some of the List API's methods by way of simply extending an AbstractList (so, adding or removing elements is unsupported), however it allows calls to set() to override ... More on stackoverflow.com
🌐 stackoverflow.com
convert arraylist into array - Java Programming
hi firstly i know this question have been asked many times before, but it seems I can't yet solve my problem on converting arraylist to array. here's what i did (i refer to the posting at http://for... More on forums.oracle.com
🌐 forums.oracle.com
September 1, 2005
Is ArrayList to Array possible?
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. More on reddit.com
🌐 r/javahelp
7
2
February 23, 2022
How do i convert array to ArrayList?
Please i need to know More on discourse.processing.org
🌐 discourse.processing.org
4
0
September 26, 2023
🌐
W3Schools
w3schools.com › java › ref_arraylist_toarray.asp
Java ArrayList toArray() Method
Java Examples Java Videos Java ... cars.toArray(); for(Object item : carsArray) { System.out.println(item); } } } ... The toArray() method returns an array containing all of the items in the list....
🌐
GeeksforGeeks
geeksforgeeks.org › java › array-to-arraylist-conversion-in-java
Array to ArrayList Conversion in Java - GeeksforGeeks
July 23, 2025 - In Java, arrays are fixed-sized, whereas ArrayLists are part of the Java collection Framework and are dynamic in nature. Converting an array to an ArrayList is a very common task and there are several ways to achieve it.
🌐
Board Infinity
boardinfinity.com › blog › how-to-convert-arraylist-to-array-in-java
Convert ArrayList to Array in Java | Board Infinity
June 16, 2026 - It is concise, type-safe, and useful in generic code because the list itself asks the generator to create an array of the right size. This variant is especially readable when converting lists of domain objects. A familiar example is converting a list of IRCTC PNR status labels into a String[] for a report template. A healthcare example is converting ArrayList<PatientReading> into PatientReading[] before feeding it into a rules module that evaluates abnormal vitals. The method is ideal in Java 11+ codebases where constructor references are already common with streams and functional interfaces.
Find elsewhere
🌐
Oracle
forums.oracle.com › ords › apexds › post › convert-arraylist-into-array-7537
convert arraylist into array - Java Programming
September 1, 2005 - hi firstly i know this question have been asked many times before, but it seems I can't yet solve my problem on converting arraylist to array. here's what i did (i refer to the posting at http://for...
🌐
W3Schools
w3schools.com › java › java_arraylist.asp
Java ArrayList
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).
🌐
CS156
ucsb-cs156.github.io › topics › java › java_arraylist_to_array.html
Java: ArrayList to array | CS156
int[] arr = this.stream().mapToInt(i -> i).toArray(); This solution uses a lambda function, which is part of the new functional programming features available in Java 8. We haven’t discussed these features in the class yet, and the HFJ book doesn’t cover them either (the book predates Java ...
🌐
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.
🌐
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 - Integer boltsInventoryArray[] = new Integer{boltsInventory.size()]; // this ensures the newly created array is of the same size as the ArrayList boltsInventoryArray = boltsInventory.toArray(boltsInventoryArray); When you change a list to array in Java like this, you are creating a deep copy. That is, all references to the Array are different from the references to the ArrayList. So you can manipulate the data in the Array without changing the data stored in the ArrayList. It is useful to convert a java list to an array when you need to test data.
🌐
Medium
anna-scott.medium.com › conversion-between-array-and-arraylist-in-java-f974c6703a27
Conversion Between Array and ArrayList in Java | by Anna Scott | Medium
February 27, 2022 - We will use method toArray() to convert our ArrayList into array. However we will run into a problem if we try to convert it into array of type int or even type Integer.
🌐
Ucsb-cs48
ucsb-cs48.github.io › javatopics › java_arraylist_to_array
Java_arraylist_to_array |
int[] arr = this.stream().mapToInt(i -> i).toArray(); This solution uses a lambda function, which is part of the new functional programming features available in Java 8. We haven’t discussed these features in the class yet, and the HFJ book doesn’t cover them either (the book predates Java ...
🌐
YouTube
youtube.com › watch
Convert an Array to an ArrayList (Java Tutorial)
Enjoy the videos and music you love, upload original content, and share it all with friends, family, and the world on YouTube.
🌐
Amelon
amelon.org › 2017 › 07 › 19 › java-convert-list-array.html
Convert between list and array in Java | Melon blog
July 19, 2017 - List<String> list = new ArrayList<>(); String[] array = (String[]) list.toArray(); We cannot new a type T array with T arr=new T[size];. So the second method returns what we want with following code. It use reflect to create the array. a.getClass().getComponentType() returns the type. public ...
🌐
DevQA
devqa.io › convert-list-to-array-in-java
Convert List to Array in Java
The best and easiest way to convert a List into an Array in Java is to use the .toArray() method.