Table of contents
1.
Introduction
2.
What Are ArrayList & Array in Java?
2.1.
Array in Java
2.2.
ArrayList in Java
2.3.
Key Differences Between Array & ArrayList
3.
Why Convert ArrayList to Array?
3.1.
Example: Converting ArrayList to Array
4.
Why Conversion Between ArrayList and Array Is Useful?
5.
Method 1: Using Object[] toArray() Method
5.1.
Syntax
5.2.
Example
6.
Method 2: Using T[] toArray(T[] a) Method
6.1.
Syntax
6.2.
Example
7.
Method 3: Manual Method Using get() Method
7.1.
Syntax
7.2.
Example
8.
Method 4: Using Streams API of Collections in Java 8 to Convert to an Array of Primitive int Type
8.1.
Syntax
8.2.
Example
9.
Real-World Use Cases
9.1.
1. Converting List Data for APIs
9.2.
2. Data Processing and Transformation
10.
Frequently Asked Questions
10.1.
Which method is best for converting an ArrayList to an array in Java?
10.2.
What happens if the array passed to toArray(T[] a) is smaller than the list size?
10.3.
Can we convert an ArrayList of primitive types directly to an array?
11.
Conclusion
Last Updated: Sep 22, 2025
Easy

Convert ArrayList to Array in Java Easily

Author Rahul Singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In Java, converting an ArrayList to an Array can be done using the toArray() method. This conversion is useful when you need a fixed-size array from a dynamic list. Java provides two ways to achieve this: using toArray() without parameters and with a specified array type. 

In this article, we will explore different methods to convert an ArrayList to an array, along with examples demonstrating their implementation.

What Are ArrayList & Array in Java?

In Java, both ArrayList & Arrays are used to store collections of data. However, they work differently & are suited for different scenarios. Let’s discuss them in detail:

Array in Java

An array is a fixed-size data structure that stores elements of the same type. Once you define the size of an array, it cannot be changed. Arrays are fast & efficient for storing & accessing data, but their lack of flexibility can be a limitation in some cases.

Let’s see how you declare & initialize an array in Java:

// Declaring an array of integers
int[] numbers = new int[5]; // This array can hold 5 integers

// Initializing the array with values
numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;
numbers[3] = 40;
numbers[4] = 50;

// Accessing elements in the array
System.out.println(numbers[2]); 

 

Output: 

30


In this example, we created an array of integers with a fixed size of 5. Once the size is set, you cannot add or remove elements beyond this limit.

ArrayList in Java

ArrayList, on the other hand, is part of Java’s Collections Framework. It is a resizable array, meaning its size can grow or shrink dynamically as you add or remove elements. This makes ArrayList more flexible than a regular array.

Let’s see how you declare & use an ArrayList:

import java.util.ArrayList; // Import the ArrayList class
public class Main {
    public static void main(String[] args) {
        // Declaring an ArrayList of integers
        ArrayList<Integer> numbers = new ArrayList<>();


        // Adding elements to the ArrayList
        numbers.add(10);
        numbers.add(20);
        numbers.add(30);
        numbers.add(40);
        numbers.add(50);


        // Accessing elements in the ArrayList
        System.out.println(numbers.get(2));


        // Printing the entire ArrayList
        System.out.println(numbers);
    }
}
You can also try this code with Online Java Compiler
Run Code

 

Output: 

30
[10, 20, 30, 40, 50]


In this example, we created an ArrayList of integers. Unlike arrays, you don’t need to specify the size upfront. You can keep adding elements, & the ArrayList will automatically resize itself.

Key Differences Between Array & ArrayList

1. Size: Arrays have a fixed size, while ArrayList can grow or shrink dynamically.
 

2. Performance: Arrays are faster for accessing elements, but ArrayList provides more flexibility.
 

3. Methods: ArrayList comes with built-in methods like `add()`, `remove()`, & `get()`, which make it easier to work with compared to arrays.

Why Convert ArrayList to Array?

While ArrayList is flexible & easy to use, there are situations where converting it to an array becomes necessary. Let’s take a look at the reasons why you might need to do this.

1. Performance Optimization

Arrays are faster than ArrayList when it comes to accessing elements. If your program requires high performance & you’re working with a fixed set of data, converting ArrayList to an array can improve efficiency.
 

2. Compatibility with Older Code

Some older Java methods or libraries might only accept arrays as input. In such cases, you’ll need to convert your ArrayList to an array to make it compatible.
 

3. Fixed-Size Requirement

If your program logic requires a fixed-size collection, an array is a better choice. Converting ArrayList to an array ensures that the size remains constant.
 

4. Memory Efficiency

Arrays consume less memory compared to ArrayList because they don’t have the overhead of additional methods & features. If memory usage is a concern, converting to an array can help.

Example: Converting ArrayList to Array

Let’s say you have an ArrayList of strings, & you need to convert it to an array. Let’s see how you can do it:

import java.util.ArrayList; // Import the ArrayList class

public class Main {
    public static void main(String[] args) {
        // Declaring an ArrayList of strings
        ArrayList<String> fruits = new ArrayList<>();


        // Adding elements to the ArrayList
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Cherry");
        fruits.add("Date");


        // Converting ArrayList to an array
        String[] fruitsArray = new String[fruits.size()]; // Create an array of the same size
        fruitsArray = fruits.toArray(fruitsArray); // Convert ArrayList to array


        // Printing the array
        for (String fruit : fruitsArray) {
            System.out.println(fruit);
        }
    }
}
You can also try this code with Online Java Compiler
Run Code


In this Code:

1. We created an ArrayList called `fruits` & added some elements to it.
 

2. We declared a string array `fruitsArray` with the same size as the ArrayList using `fruits.size()`.
 

3. We used the `toArray()` method to convert the ArrayList to an array.
 

4. Finally, we printed the elements of the array using a `for-each` loop.


Output:

Apple
Banana
Cherry
Date


This example shows how easy it is to convert an ArrayList to an array in Java. The `toArray()` method is the key here, & it ensures that the conversion is done smoothly.

Why Conversion Between ArrayList and Array Is Useful?

In Java development, converting an ArrayList to an array is often necessary when working with tools, libraries, or APIs that don’t support collections. Older or third-party code may only accept arrays like String[] or Integer[], requiring conversion for compatibility. This is also useful when exporting data to files, serializing it, or passing it to performance-critical functions where arrays are faster and use less memory. Additionally, arrays are sometimes easier to handle in simple loops or low-level operations. This conversion ensures smooth integration between modern and legacy components in real-world applications.

Method 1: Using Object[] toArray() Method

This method returns an array containing all elements of the ArrayList. The array's runtime type is Object[], meaning it does not preserve the actual data type of elements.

Syntax

Object[] array = arrayList.toArray();

Example

import java.util.ArrayList;
public class ArrayListToArrayExample1 {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("Apple");
        list.add("Banana");
        list.add("Cherry");  
        Object[] array = list.toArray();  
        for (Object obj : array) {
            System.out.println(obj);
        }
    }
}
You can also try this code with Online Java Compiler
Run Code


Output

Apple
Banana
Cherry


This method is useful but does not preserve the actual data type, which might require explicit type casting.

Method 2: Using T[] toArray(T[] a) Method

This method provides type safety and returns an array of the specified type. If the provided array is large enough, elements are stored in it; otherwise, a new array is created.

Syntax

T[] array = arrayList.toArray(new T[0]);

Example

import java.util.ArrayList;
public class ArrayListToArrayExample2 {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("Java");
        list.add("Python");
        list.add("C++");    
        String[] array = list.toArray(new String[0]); 
        for (String str : array) {
            System.out.println(str);
        }
    }
}
You can also try this code with Online Java Compiler
Run Code


Output:

Java
Python
C++


This method is recommended as it ensures type safety and avoids explicit type casting.

Method 3: Manual Method Using get() Method

We can manually copy elements from an ArrayList to an array using a loop and the get() method.

Syntax

T[] array = new T[arrayList.size()];
for (int i = 0; i < arrayList.size(); i++) {
    array[i] = arrayList.get(i);
}

Example

import java.util.ArrayList;
public class ArrayListToArrayExample3 {
    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3); 
        Integer[] array = new Integer[list.size()];
        for (int i = 0; i < list.size(); i++) {
            array[i] = list.get(i);
        }   
        for (int num : array) {
            System.out.println(num);
        }
    }
}
You can also try this code with Online Java Compiler
Run Code


Output:

1
2
3


This method provides more control over the conversion process but is less efficient than built-in methods.

Method 4: Using Streams API of Collections in Java 8 to Convert to an Array of Primitive int Type

Java 8 introduced Streams, which provide a modern way to convert an ArrayList to an array. This method is efficient and useful for working with primitive data types.

Syntax

int[] array = arrayList.stream().mapToInt(Integer::intValue).toArray();

Example

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class ArrayListToArrayExample4 {
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<>();
        list.add(10);
        list.add(20);
        list.add(30); 
        int[] array = list.stream().mapToInt(Integer::intValue).toArray();
        for (int num : array) {
            System.out.println(num);
        }
    }
}
You can also try this code with Online Java Compiler
Run Code


Output:

10
20
30


This method is the best option when dealing with primitive types since it avoids unnecessary boxing and unboxing.

Real-World Use Cases

1. Converting List Data for APIs

In Java, some third-party APIs or legacy systems accept only arrays, not ArrayList objects. Developers often need to convert an ArrayList to an array to ensure compatibility.
Example:

List<String> namesList = Arrays.asList("Alice", "Bob", "Charlie");
String[] namesArray = namesList.toArray(new String[0]);

This conversion is essential when integrating with APIs that require array-based parameters for processing.

2. Data Processing and Transformation

In batch processing or data transformation tasks, using arrays can be more memory-efficient than lists. Converting a dynamic ArrayList to a fixed-size array improves performance and simplifies downstream processing, especially when working with large datasets.
Example:

List<Integer> numbersList = Arrays.asList(1, 2, 3, 4, 5);
Integer[] numbersArray = numbersList.toArray(new Integer[0]);
// Pass the array to a batch processor
processData(numbersArray);

This approach is often used for optimizing processing speed in high-volume data pipelines.

Frequently Asked Questions

Which method is best for converting an ArrayList to an array in Java?

The best method depends on the scenario- Use toArray(new T[0]) for type safety, Use Streams API for primitive types or Use toArray() if type safety is not a concern.

What happens if the array passed to toArray(T[] a) is smaller than the list size?

A new array of the same type but with the required size is created and returned.

Can we convert an ArrayList of primitive types directly to an array?

No, ArrayList stores objects, not primitives. You must use Java 8 Streams to convert an ArrayList<Integer> to int[].

Conclusion

In this article, we learned how to convert an ArrayList to an Array in Java. Java provides methods like toArray() to achieve this conversion easily. Understanding this process is important when working with collections and arrays, as it helps in efficient data manipulation and compatibility between different data structures.

Live masterclass