The Wayback Machine - https://web.archive.org/web/20200420062926/https://www.geeksforgeeks.org/arraylist-array-conversion-java-toarray-methods/amp/

ArrayList to Array Conversion in Java : toArray() Methods

Following methods can be used for converting ArrayList to Array:


Method 1: Using Object[] toArray() method




Syntax:

 public Object[] toArray() 
filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate working of
// Objectp[] toArray()
import java.io.*;
import java.util.List;
import java.util.ArrayList;
  
class GFG
{
    public static void main (String[] args)
    {
        List<Integer> al = new ArrayList<Integer>();
        al.add(10);
        al.add(20);
        al.add(30);
        al.add(40);
  
        Object[] objects = al.toArray();
  
        // Printing array of objects
        for (Object obj : objects)
            System.out.print(obj + " ");
    }
}
chevron_right

Output:

10 20 30 40 

Note: toArray() method returns an array of type Object(Object[]). We need to typecast it to Integer before using as Integer objects. If we do not typecast, we get compilation error. Consider the following example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// A Java program to demonstrate that assigning Objects[]
// to Integer[] causes error.
import java.io.*;
import java.util.List;
import java.util.ArrayList;
  
class GFG
{
    public static void main (String[] args)
    {
        List<Integer> al = new ArrayList<Integer>();
        al.add(10);
        al.add(20);
        al.add(30);
        al.add(40);
  
        // Error: incompatible types: Object[]
        // cannot be converted to Integer[]
        Integer[] objects = al.toArray();
  
        for (Integer obj : objects)
            System.out.println(obj);
    }
}
chevron_right

Output:

19: error: incompatible types: Object[] cannot be converted to Integer[]
        Integer[] objects = al.toArray(); 
                                      ^
1 error

It is therefore recommended to create an array into which elements of List need to be stored and pass it as an argument in toArray() method to store elements if it is big enough. Otherwise a new array of the same type is allocated for this purpose.

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


// Converts a list into an array arr[] and returns same. 
// If arr[] is not big enough, then a new array of same
// type is allocated for this purpose.
// T represents generic.
public  T[] toArray(T[] arr)

Note that the there is an array parameter and array return value. The main purpose of passed array is to tell the type of array. The returned array is of same type as passed array.

It throws ArrayStoreException if the runtime type of a is not a supertype of the runtime type of every element in this list.

filter_none

edit
close

play_arrow

link
brightness_4
code

// A Java program to convert an ArrayList to arr[]
import java.io.*;
import java.util.List;
import java.util.ArrayList;
  
class GFG
{
    public static void main(String[] args)
    {
        List<Integer> al = new ArrayList<Integer>();
        al.add(10);
        al.add(20);
        al.add(30);
        al.add(40);
  
        Integer[] arr = new Integer[al.size()];
        arr = al.toArray(arr);
  
        for (Integer x : arr)
            System.out.print(x + " ");
    }
}
chevron_right

Output:

10 20 30 40

Note : If the specified array is null then it will throw NullpointerException. See this for example.



Method 3: Manual method to convert ArrayList using get() method

We can use this method if we don’t want to use java in built toArray() method. 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)
filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to convert a ArrayList to an array
// using get() in a loop.
import java.io.*;
import java.util.List;
import java.util.ArrayList;
  
class GFG
{
    public static void main (String[] args)
    {
        List<Integer> al = new ArrayList<Integer>();
        al.add(10);
        al.add(20);
        al.add(30);
        al.add(40);
  
        Integer[] arr = new Integer[al.size()];
  
        // ArrayList to Array Conversion
        for (int i =0; i < al.size(); i++)
            arr[i] = al.get(i);
  
        for (Integer x : arr)
            System.out.print(x + " ");
    }
}
chevron_right

Output

10 20 30 40

Reference:
http://docs.oracle.com/javase/1.5.0/docs/api/java/util/ArrayList.html#toArray()

This article is contributed by Nitsdheerendra. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.





Improved By : nidhi_biet

Article Tags :
Practice Tags :