Java ArrayList.toArray(T[] a)

Syntax

ArrayList.toArray(T[] a) has the following syntax.

public <T> T[] toArray(T[] a)

Example

In the following code shows how to use ArrayList.toArray(T[] a) method.


/*from w  ww  .ja v a 2s. c o m*/

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

public class Main {
   public static void main(String[] args) {
      
    ArrayList<Integer>  arrlist = new ArrayList<Integer> ();

    arrlist.add(10);
    arrlist.add(12);
    arrlist.add(31);
    arrlist.add(49);
  
    // toArray copies content into other array
    Integer myArray[] = new Integer[arrlist.size()];
    myArray = arrlist.toArray(myArray);

    System.out.println(Arrays.toString(myArray));

  }
}

The code above generates the following result.