How to convert an ArrayList to array in java

Many times arises a situation where one needs to convert an ArrayList to array. One of the most common scenarios is when executing a method from a third party library which accepts an array while the code calling this method has an ArrayList. There are a couple of methods to do this, one is a simpler approach by iterating over the list while second one is by using java api methods. Both will be discussed in this post.

Method 1 : Iterating over the ArrayList

This is a simpler approach which involves creating an array equal to the size of the list, iterating the list and adding element at every index of the list to the array. Remember, the array should be of the same or compatible type as the type of ArrayList otherwise there may be compiler error or a java.lang.ClassCastException at runtime.

import java.util.List;
import java.util.ArrayList;
 
public class ListToArrayConverter {
    public static void main(String[] args) {
        // create the list
        List list = new ArrayList();
        // add elements to list
        list.add("Java");
        list.add("C#");
        list.add("Ruby");
        list.add("VB.NET");   
        list.add("Python");
        // get the size of list
        int size = list.size();
        // create an array of same size  
        String[] languages = new String[size]; 
        for(int i = 0; i < size; i++) {
           // get the element at current list index to the array   
           languages[i] = list.get(i);
        }
    }
}

Output

Java
C#
Ruby
VB.NET

Method 2 : Using toArray method of java api

java.util.List has a toArray method which takes an array as argument. The type of this array must be same as the type of List. Also the size of the array should be of the same size as that of the list.

import java.util.List;
import java.util.ArrayList;
 
public class ListToArrayConverter {
    public static void main(String[] args) {
        // create the list
        List list = new ArrayList();
        // add elements to list
        list.add("Java");
        list.add("C#");
        list.add("Ruby");
        list.add("VB.NET");   
        list.add("Python");
        // get the size of list
        int size = list.size();
        // create an array of same size  
        String[] languages = new String[size]; 
        // convert list to array
        list.toArray(languages); 
        // print array contents 
        for(int i = 0; i < size; i++) {
           System.out.println(languages[i]);
        } 
    }
}

Output

Java
C#
Ruby
VB.NET

Code to convert list to array given above can be written in one line as
list.toArray(new String[list.size()]);

 

Refer this post to learn different ways to convert array to list

Let’s tweak in

  1. If the type of array supplied to toArray method is different from the type of list, then a java.lang.ArrayStoreException is raised at run time.
  2. If the array supplied to the toArray is null then a java.lang.NullPointerException is raised.
  3. If the size of the array is greater than the elements in the list, then the remaining excess elements of the array will be null.
  4. If the size of the array is lesser than the elements in the list, then all the elements of the array are null.
  5. toArray method is declared in java.util.List which is an interface and implemented in java.util.ArrayList.

 

Mark Your Impression

Close Menu