ArrayList Object[] toArray() method in java

Let’s learn ArrayList Object[] toArray() method in java.

ArrayList Object[] toArray() method in java

Object[] toArray() method of ArrayList class returns an array containing all of the elements in this list in proper sequence (from first to last element).

This method acts as bridge between array-based and collection-based APIs.

Syntax:

public Object[] toArray()

Specified by:

toArray() in Collection.

Also read – ArrayList in java

Now let’s see an example on ArrayList Object[] toArray() method.

import java.util.ArrayList;
import java.util.List;
public class ArrayListObjectToArrayMethodExample
{
   public static void main(String[] args)
   {
      List<Integer> al = new ArrayList<Integer>();
      al.add(12);
      al.add(14);
      al.add(16);
      al.add(18);
      al.add(20);
      Object[] obj = al.toArray();
      for(Object num : obj)
      {
         System.out.print(num + " ");
      }
   }
}


Output:

12 14 16 18 20