Java ArrayList toArray() – Convert ArrayList to Array

Learn to convert ArrayList to array using toArray() method with example. toArray() method returns an array containing all of the elements in the list in proper sequence (from first to last element).

1. ArrayList toArray() syntax

toArray() is overloaded method and comes in two forms:

public Object[] toArray();
public <T> T[] toArray(T[] a);
  1. The first method does not accept any argument and returns the array of object type. We must iterate the objects array to find the desired element and typecast to desired class type.
  2. In second method, the runtime type of the returned array is that of the specified array. If the list fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this list.

    After filling all array elements, it there is more space left in array then 'null' is populated in all those spare positions.

2. ArrayList toArray() example to convert ArrayList to Array

2.1. ArrayList toArray() – convert to object array

Java program to convert an arraylist to object array and iterate through array content.

import java.util.ArrayList;
import java.util.Arrays;

public class ArrayListExample 
{
    public static void main(String[] args) 
    {
        ArrayList<String> list = new ArrayList<>(2);
        
        list.add("A");
        list.add("B");
        list.add("C");
        list.add("D");
        
        //Convert to object array
        Object[] array = list.toArray();
        
        System.out.println( Arrays.toString(array) );

        //Iterate and convert to desired type
        for(Object o : array) {
            String s = (String) o;
            
            System.out.println(s);
        }
    }
}

Program output.

[A, B, C, D]

A
B
C
D

2.2. ArrayList toArray(T[] a) – convert to string array

Java program to convert an arraylist to string array.

 a) example"]
import java.util.ArrayList;
import java.util.Arrays;

public class ArrayListExample 
{
    public static void main(String[] args) 
    {
        ArrayList<String> list = new ArrayList<>(2);
        
        list.add("A");
        list.add("B");
        list.add("C");
        list.add("D");
        
        //Convert to string array
        String[] array = list.toArray(new String[list.size()]);
        
        System.out.println(Arrays.toString(array));
    }
}

Program output.

[A, B, C, D]

Happy Learning !!

Read More:

A Guide to Java ArrayList
ArrayList Java Docs

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

2 thoughts on “Java ArrayList toArray() – Convert ArrayList to Array”

  1. hey nice post. I was just wondering if you could tell me how to return an int[] type array from a method in java if I have a ArrayList type list. Is there any in-built methods to do that, the above two methods are not helping me.

    Reply
    • Java does not provide a straightforward solution for this. Lists being a collection of objects, if we need primitives out of it, we need to do it ourselves.

      1. Use Stream API

      int[] intArray = listOfIntegers.stream().mapToInt(Integer::intValue).toArray();

      2. Use Google Guava

      int[] intArray = Ints.toArray(listOfIntegers);
      Reply

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.