Home » Java programming language

Java ArrayList toArray() Method with Example

ArrayList Class toArray() method: Here, we are going to learn about the toArray() method of ArrayList Class with its syntax and example.
Submitted by Preeti Jain, on January 19, 2020

ArrayList Class toArray() method

Syntax:

    public Object[] toArray();
    public T[] toArray(T[] elements);
  • toArray() method is available in java.util package.
  • toArray() method is used to convert the given Arraylist to an array or in other words, this method is used to return an array that contains all the elements in this Arraylist.
  • toArray(T[] elements) method is used to return an array of the runtime type is that of the given array T[], when this Arraylist fits in the given array then the same array will be returned else a new array is allocated is the type of the given array.
  • toArray() method does not throw an exception at the time of returning an array.
  • toArray(T[] elements) method may throw an exception at the time of returning an array.
    • ArrayStoreException: This exception may throw when the dynamic type of the given array T[] is not a parent type of the dynamic type of element in this Arraylist.
    • NullPointerException: This exception may throw when the given array is null exists.
  • These are non-static methods, it is accessible with the class object and if we try to access these methods with the class name then we will get an error.

Parameter(s):

  • In the first case, toArray(): It does not accept any parameter.
  • In the Second case, toArray(T[] elements):
    T[] elements – represents the array to store elements, when it is capable to store else it creates a new array according to its size of the same dynamic type.

Return value:

In the first case, The return type of the method is Object(), it returns an array of Object type that contains all the elements in this Arraylist.

In the second case, The return type of the method is T[], it returns an array that contains all the elements of this array.

Example:

// Java program to demonstrate the example 
// of void toArray() method of ArrayList

import java.util.*;

public class ToArrayOfArrayList {
    public static void main(String args[]) {
        // Create an ArrayList with initial capacity
        // to store elements
        ArrayList < String > arr_l = new ArrayList < String > (10);
        String str_l[] = new String[4];

        // By using add() method is to add elements
        // in the ArrayList
        arr_l.add("C");
        arr_l.add("C++");
        arr_l.add("Java");
        arr_l.add("DotNet");

        // Display ArrayList 
        System.out.println("ArrayList Elements :" + arr_l);

        System.out.println();

        // Display String Array
        for (String s: str_l)
            System.out.println("str_l :" + s);

        // By using toArray() method is to convert the
        // collection to Array
        Object[] o = arr_l.toArray();

        System.out.println();

        // Display ArrayList
        for (Object val: arr_l)
            System.out.println("arr_l.toArray() : " + val);

        // By using toArray(T[]) method is to coipies the
        // collection to the given Array
        str_l = arr_l.toArray(str_l);

        System.out.println();

        // Display str_l
        for (String val1: str_l)
            System.out.println("arr_l.toArray(str_l) : " + val1);
    }
}

Output

ArrayList Elements :[C, C++, Java, DotNet]

str_l :null
str_l :null
str_l :null
str_l :null

arr_l.toArray() : C
arr_l.toArray() : C++
arr_l.toArray() : Java
arr_l.toArray() : DotNet

arr_l.toArray(str_l) : C
arr_l.toArray(str_l) : C++
arr_l.toArray(str_l) : Java
arr_l.toArray(str_l) : DotNet



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.