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);
Discussions

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
What is the best way to convert an array to an arraylist?
Looks as simple as List newList = Array.asList(yourOldArray); https://www.geeksforgeeks.org/conversion-of-array-to-arraylist-in-java/ More on reddit.com
🌐 r/learnjava
17
13
February 18, 2019
Converting an array to an array list?
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
6
2
July 24, 2021
Converting Arrays to Arraylist
In this case, Arrays.asList() produces a list of arrays, i.e., List. A normal iterative approach is best to populate the list. It may also be possible to do this with Streams, if you can use those. More on reddit.com
🌐 r/javahelp
7
10
April 26, 2022
🌐
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); } } }
🌐
GeeksforGeeks
geeksforgeeks.org › java › conversion-of-array-to-arraylist-in-java
Conversion of Array To ArrayList in Java - GeeksforGeeks
July 23, 2025 - We can use this method if we don’t want to use java in built method(s). This is a manual method of adding all array's elements to List. Syntax: public boolean add(Object obj) // Appends the specified element to the end of this list. // Returns true. ... // Java program to convert a ArrayList to // an array using add() in a loop.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › ArrayList.html
ArrayList (Java Platform SE 8 )
April 21, 2026 - Returns a shallow copy of this ArrayList instance. (The elements themselves are not copied.) ... Returns an array containing all of the elements in this list in proper sequence (from first to last element).
🌐
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.
Find elsewhere
🌐
W3Schools
w3schools.com › java › java_arraylist.asp
Java ArrayList
How Tos Add Two Numbers Swap Two Variables Even or Odd Number Reverse a Number Positive or Negative Square Root Area of Rectangle Celsius to Fahrenheit Sum of Digits Check Armstrong Num Random Number Count Words Count Vowels in a String Remove Vowels Count Digits in a String Reverse a String Palindrome Check Check Anagram Convert String to Array Remove Whitespace Count Character Frequency Sum of Array Elements Find Array Average Sort an Array Find Smallest Element Find Largest Element Second Largest Array Min and Max Array Merge Two Arrays Remove Duplicates Find Duplicates Shuffle an Array Factorial of a Number Fibonacci Sequence Find GCD Check Prime Number ArrayList Loop HashMap Loop Loop Through an Enum
🌐
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 ...
🌐
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.
🌐
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 - You should remember how to initialize an array in java; it looks like this: datatype[] isArray; Where datatype is any primitive data type like int, float, long, or even String. You can also place the brackets after the declaration like so: datatype isArray[]; So how do we take this static array with a set number of elements and convert it to an ArrayList?
🌐
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 › 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 ...
🌐
Reddit
reddit.com › r/javahelp › converting an array to an array list?
r/javahelp on Reddit: Converting an array to an array list?
July 24, 2021 -

Can’t figure out what I’m doing wrong! I’m in an intro to Java class. The assignment is supposed to be adding an additional language to the array list . When I run the program I get an

“Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: index 8 out of bounds for length 7”

This is most of my code:        *edited because my coding didn’t paste correctly. I’ve added a pastebin link with my code

pastebin link

Top answer
1 of 3
4
Array and ArrayList is not the same thing. From your code you define an Array of Strings with some values and then afterwards you try to expand and add another value to it. This is one of the differences of Array and ArrayList, to expand an Array you need to create a new Array, ArrayList on the other hand is flexible and can add and remove items. So to solve the assignment you need to take the values from the Array and put in an ArrayList. Then you can add additional values to the ArrayList if you want.
2 of 3
2
So, you said, "The assignment is supposed to be adding an additional language to the array list". But you don't have an array list in your program, you have an array. An array of five Strings looks like this: String[] strArr = new String[5]; An ArrayList of Strings looks like this: ArrayList strList = new ArrayList<>(); Notice that I didn't specify a size for the ArrayList. This is because Java will increase the size of the list automatically as you add items. However, this is NOT true for arrays. An array is a fixed size, and you can't add anything to it. So in this case, it depends on what your assignment specifications are. If you are required to use an array, then you need to ask the user how many items they will be adding, and THEN declare the array with that size, BEFORE adding the elements to it. If you can use an ArrayList, then I would use that because it is simpler, you can just keep adding elements like so: strList.add("Pascal"). There is another issue with your code that is separate from your question. You are declaring the array on lines 8 and 9 with a fixed set of languages, but then you overwrite all of those in your loop using input from the user. There is no reason to do this, and it is wasteful. If you are going to get the list of languages from the user, then just declare it as String[] languagesArray = new String[7]; (or however many you are going to need).
🌐
Board Infinity
boardinfinity.com › blog › how-to-convert-arraylist-to-array-in-java
Convert ArrayList to Array in Java | Board Infinity
December 9, 2022 - 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.
🌐
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 ...
🌐
Baeldung
baeldung.com › home › java › java array › converting between an array and a list in java
Converting Between an Array and a List in Java Baeldung
August 6, 2025 - List<Integer> targetList = new ArrayList<Integer>(Arrays.asList(sourceArray)); Now let’s use the Guava API for the same conversion: @Test public void givenUsingGuava_whenArrayConvertedToList_thenCorrect() { Integer[] sourceArray = { 0, 1, 2, 3, 4, 5 }; List<Integer> targetList = Lists.newArrayList(sourceArray); } Finally, let’s use the Apache Commons Collections CollectionUtils.addAll API to fill in the elements of the array in an empty List:
🌐
LabEx
labex.io › tutorials › java-how-to-create-an-arraylist-from-an-array-in-java-414983
How to create an ArrayList from an array in Java | LabEx
In Java, the ArrayList is a widely used data structure that provides a dynamic and flexible way to store and manipulate collections of elements. This tutorial will guide you through the process of converting an array to an ArrayList, enabling you to leverage the benefits of this versatile data structure in your Java programming projects.
🌐
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.
🌐
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 - Let’s first look into converting ArrayList into array. We will start by creating an ArrayList of integers 1 through 4. Since only objects could be stored in ArrayList, Java will use autoboxing with wrapper class Integer to turn our primitive integers into objects of class Integer.