typecast list to arraylisst code example

Example 1: ArrayList Object[] toArray() method in java

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 + " ");
      }
   }
}

Example 2: array to arraylist

new ArrayList<>(Arrays.asList(array));

Tags:

Java Example