Arraylist.toarray example – Java ArrayList toArray() Method with Example

Arraylist.toarray example: In the previous article we have discussed about Java ArrayList toString() Method with Example

In this article we are going to see the use of ArrayList toArray() method along with suitable examples by using Java programming language.

Java ArrayList toArray() Method with Example

toArray():

This java.util.ArrayList.toArray() method is used to convert an ArrayList into an Array.

It returns the same ArrayList elements but in the form of Array.

Let’s see toArray() method with suitable examples.

Method-1: Java ArrayList toArray(arr) Method

Syntax:

arrayListName.toArray(Array arr)

Where,

  • arrayListName refers to the name of your ArrayList.
  • Array arr refers to an array which will contain the arraylist elements in array format.

Approach:

  • Create a new ArrayList of type String.
  • Add string elements into the ArrayList using the add() method.
  • Display the ArrayList elements.
  • Then create an array of same String type and same size of the ArrayList.
  • Then convert the ArrayList to an Array using toArray() method.
  • Print the elements inside the Array using for each loop.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        // Create an ArrayList of string datatype
        ArrayList<String> arr = new ArrayList<String>();
        // Adding some elements to the ArrayList
        arr.add("Apple");
        arr.add("boy");
        arr.add("Dog");
        arr.add("egg");
        arr.add("Flower");
        // Prints the ArrayList elements
        System.out.println("The elements of ArrayList are: "+arr);
        // Create a new array of String type
        // size of array is same as the ArrayList
        String[] arr1 = new String[arr.size()];
        // Convert ArrayList into an array
        arr.toArray(arr1);
        // print all elements of the array
        System.out.print("After converted to Array: ");
        for(String elements:arr1)
        System.out.print(elements+"  ");
    }
}
Output:

The elements of ArrayList are: [Apple, boy, Dog, egg, Flower]
After converted to Array: Apple boy Dog egg Flower

Method-2: Java ArrayList toArray() Method

Syntax:

arrayListName.toArray()

Where,

  • arrayListName refers to the name of your ArrayList.

Approach:

  • Create a new ArrayList of type String.
  • Add Integer elements into the ArrayList using the add() method.
  • Display the ArrayList elements.
  • Then create an array without a parameter
  • Then convert the ArrayList to an Array using toArray() method.
  • Print the elements inside the Array using for each loop.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {        
        // Create an ArrayList of Integer datatype
        ArrayList<Integer> arr = new ArrayList<Integer>();
        // Adding some elements to the ArrayList
        arr.add(2);
        arr.add(52);
        arr.add(13);
        arr.add(17);
        arr.add(1);
        // Prints the ArrayList elements
        System.out.println("The elements of ArrayList are: "+arr);
        // Convert ArrayList into an array
        // the method has no parameter
        Object[] obj = arr.toArray();
        // print all elements of the array
        System.out.print("After converted to Array: ");
        for(Object element : obj)
        System.out.print(element+"  ");
    }
}
Output:

The elements of ArrayList are: [2, 52, 13, 17, 1]
After converted to Array: 2 52 13 17 1

Grab the opportunity to learn all effective java programming language concepts from basic to advance levels by practicing these Java Program Examples with Output.

Related Java Programs: