ArrayList toArray() method in Java with Examples - GeeksforGeeks

20 Dec, 2018
The toArray() method of ArrayList is used to return an array containing all the elements in ArrayList in the correct order.
Syntax:
public Object[] toArray()
           or
public <T> T[] toArray(T[] a)
Parameters: This method either accepts no parameters or it takes an array T[] a as parameter which is the array into which the elements of the list are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.
Return Value: The function returns an array containing all the elements in this list.
Exception: The first overload of this method throws no exceptions. However, the second overload throws following exceptions:
ArrayStoreException: if the runtime type of the specified array is not a supertype of the runtime type of every element in this list.
NullPointerException if the specified array is null
Below programs illustrate the ArrayList.toArray() method:
Program 1:
// Java Program to illustrate the// ArrayList toArray() method in Java  import java.util.*;  public class GFG {    public static void main(String[] args)    {          // create object of ArrayList        ArrayList<Integer> ArrLis            = new ArrayList<Integer>();          // Add elements        ArrLis.add(32);        ArrLis.add(67);        ArrLis.add(98);        ArrLis.add(100);          // print ArrayList        System.out.println("ArrayList: "                           + ArrLis);          // Get the array of the elements        // of the ArrayList        // using toArray() method        Object[] arr = ArrLis.toArray();          System.out.println("Elements of ArrayList"                           + " as Array: "                           + Arrays.toString(arr));    }}
ArrayList: [32, 67, 98, 100]
Elements of ArrayList as Array: [32, 67, 98, 100]

Program 2:
// Java Program to illustrate the// ArrayList toArray(T[]) method in Java  import java.util.*;  public class GFG {    public static void main(String[] args)    {          // create object of ArrayList        ArrayList<Integer> ArrLis            = new ArrayList<Integer>();          // Add elements        ArrLis.add(32);        ArrLis.add(67);        ArrLis.add(98);        ArrLis.add(100);          // print ArrayList        System.out.println("ArrayList: "                           + ArrLis);          // Get the array of the elements        // of the ArrayList        // using toArray(T[]) method        Integer arr[] = new Integer[ArrLis.size()];        arr = ArrLis.toArray(arr);          System.out.println("Elements of ArrayList"                           + " as Array: "                           + Arrays.toString(arr));    }}
ArrayList: [32, 67, 98, 100]
Elements of ArrayList as Array: [32, 67, 98, 100]

Reference:
https://docs.oracle.com/javase/9/docs/api/java/util/ArrayList.html#toArray–
https://docs.oracle.com/javase/9/docs/api/java/util/ArrayList.html#toArray-T:A-
Java - util package
Java-ArrayList
Java-Arrays
Java-Collections
Java-Functions
Picked
Java
Java
Java-Collections
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Object Oriented Programming (OOPs) Concept in Java
HashMap in Java with Examples
Stream In Java
Interfaces in Java
How to iterate any Map in Java
ArrayList in Java
Initialize an ArrayList in Java
Stack Class in Java
Singleton Class in Java
Multidimensional Arrays in Java