Java How to Convert List to Array with Example
In Java, convert a List to an array by calling
list.toArray(new Type[0]) or list.toArray(new Type[list.size()]), where Type is the array element type.Examples
Input["apple", "banana", "cherry"]
Output["apple", "banana", "cherry"]
Input[]
Output[]
Input[1, 2, 3, 4]
Output[1, 2, 3, 4]
How to Think About It
To convert a List to an array, you create a new array of the same type and size as the List, then use the List's
toArray method to fill this array with the List's elements. This ensures the array has the correct type and length.Algorithm
1
Get the List you want to convert.2
Create a new array of the desired type with the same size as the List.3
Call the List's toArray method, passing the new array as an argument.4
The method fills the array with List elements and returns it.5
Use or return the filled array.Code
java
import java.util.*; public class ListToArrayExample { public static void main(String[] args) { List<String> fruits = Arrays.asList("apple", "banana", "cherry"); String[] fruitsArray = fruits.toArray(new String[fruits.size()]); System.out.println(java.util.Arrays.toString(fruitsArray)); } }
Output
[apple, banana, cherry]
Dry Run
Let's trace converting List ["apple", "banana", "cherry"] to array.
1
Create List
List fruits = ["apple", "banana", "cherry"]
2
Create Array
String[] fruitsArray = new String[3]
3
Call toArray
fruits.toArray(fruitsArray) fills fruitsArray with ["apple", "banana", "cherry"]
| Step | Array Content |
|---|---|
| After Step 2 | [null, null, null] |
| After Step 3 | [apple, banana, cherry] |
Why This Works
Step 1: Create Array of Correct Size
You create a new array with the same size as the List to hold all elements.
Step 2: Use toArray Method
The List's toArray method copies elements into the new array.
Step 3: Return Filled Array
The method returns the array filled with List elements, ready to use.
Alternative Approaches
Using toArray() without argument
java
import java.util.*; public class ListToArrayAlt { public static void main(String[] args) { List<String> fruits = Arrays.asList("apple", "banana", "cherry"); Object[] fruitsArray = fruits.toArray(); System.out.println(java.util.Arrays.toString(fruitsArray)); } }
Returns an Object array, so you may need to cast elements; less type-safe.
Using Stream API
java
import java.util.*; public class ListToArrayStream { public static void main(String[] args) { List<String> fruits = Arrays.asList("apple", "banana", "cherry"); String[] fruitsArray = fruits.stream().toArray(String[]::new); System.out.println(java.util.Arrays.toString(fruitsArray)); } }
Uses streams, more flexible but slightly more complex.
Complexity: O(n) time, O(n) space
Time Complexity
The method copies each element once, so time grows linearly with the List size.
Space Complexity
A new array of the same size as the List is created, so space grows linearly.
Which Approach is Fastest?
Using toArray(new Type[size]) is fastest and type-safe; using streams adds overhead.
| Approach | Time | Space | Best For |
|---|---|---|---|
| toArray(new Type[size]) | O(n) | O(n) | Type-safe, efficient conversion |
| toArray() | O(n) | O(n) | Quick but returns Object[], less type-safe |
| Stream API | O(n) | O(n) | Flexible, functional style, slight overhead |
Always pass a correctly sized array to
toArray for type safety and performance.Not passing an array of the right type causes the method to return an Object array, which may require casting.