Java ArrayList toArray() Method

Java ArrayList Java ArrayList

The toArray() method converts an ArrayList object into an array.

The syntax of the toArray() method is:

arraylist.toArray(T[] arr)

Note:arraylist is an object of the ArrayList class.

Parameters:

  • T [] arr(optional parameter) - the array used to store the array elements

Note:Here, T refers to the type of the array.

Return Value

If the parameterT[] arris passed as a parameter into the method, an array of type T is returned.

If no parameter is passed, an array of type Object is returned.

Example

Array parameter of a specified type:

Example

import java.util.ArrayList;
import java.util.Comparator;
class Main {
    public static void main(String[] args){

        // Create a dynamic array
        ArrayList<String> sites = new ArrayList<>();
       
        sites.add("Example");
        sites.add("Google");
        sites.add("Wiki");
        sites.add("Taobao");
        System.out.println("Website list: " + sites);

        // Create a new array of type String
        // The array length is the same as the ArrayList length
        String[] arr = new String[sites.size()];

        // Convert the ArrayList object into an array
        sites.toArray(arr);

        // Output all elements of the array
        System.out.print("Array: ");
        for(String item:arr) {
            System.out.print(item+", ");
        }
    }
}

Executing the above program produces the following output:

网站列表: [Example, Google, Wiki, Taobao]
Array: Example, Google, Wiki, Taobao, 

In the above example, we used the sort() method on the array named sites.

Note this line:

sites.toArray(arr);

Here, we passed an array of type String as a parameter, and all elements are stored in the string array.

Note:The length of the array passed as a parameter should be equal to or greater than the arraylist. Here, we usedArrayList size()method to create an array with the same size as the arraylist.

Using the toArray() method without parameters:

Example

import java.util.ArrayList;
import java.util.Comparator;
class Main {
    public static void main(String[] args){

        // Create a dynamic array
        ArrayList<String> sites = new ArrayList<>();
       
        sites.add("Example");
        sites.add("Google");
        sites.add("Wiki");
        sites.add("Taobao");
        System.out.println("Website list: " + sites);

        // Convert the ArrayList object into an array
        // This method takes no parameters
        Object[] obj = sites.toArray();

        // Output all elements in the array
        System.out.print("Array: ");
        for(Object item : obj) {
            System.out.print(item+", ");
        }
    }
}

Executing the above program produces the following output:

网站列表: [Example, Google, Wiki, Taobao]
Array: Example, Google, Wiki, Taobao,

In the above example, we used toArray() to convert the arraylist into an array.

Note:It is recommended to use the toArray() method with parameters.

Java ArrayList Java ArrayList

Other Extensions