The toArray() method of ArrayList is used to convert the list into an array while preserving the order of elements. It is commonly used when array-based processing is required or when interacting with APIs that work with arrays instead of collections.
Method Declaration
public Object[] toArray()
or
public <T> T[] toArray(T[] a)
Parameters:
- toArray() -> No parameters
- toArray(T[] a) -> a is the array into which list elements are stored; if it is not large enough, a new array of the same type is returned
Return Type:
- Object[] -> For the no-argument version
- T[] -> For the generic version, containing all elements of the ArrayList
Examples of Java ArrayList.toArray() method
Example 1 :Converting an ArrayList to an Array using toArray() Method in Java
The following Java program demonstrates how to convert an ArrayList to an array using the toArray() method. This example showcases the basic usage of the ArrayList and how its elements can be retrieved and stored in the array.
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// create object of ArrayList
ArrayList<Integer> ArrLis
= new ArrayList<Integer>();
// Add elements
ArrLis.add(32);
ArrLis.add(67);
ArrLis.add(98);
ArrLis.add(100);
// print ArrayList
System.out.println("ArrayList: " + ArrLis);
// Get the array of the elements
// of the ArrayList
// using toArray() method
Object[] arr = ArrLis.toArray();
System.out.println("Elements of ArrayList"
+ " as Array: "
+ Arrays.toString(arr));
}
}
Output
ArrayList: [32, 67, 98, 100] Elements of ArrayList as Array: [32, 67, 98, 100]
Explanation:
- This Java program demonstrates the use of
ArrayList'stoArray()method to convert its elements into a regular array. - First, integers are added to the ArrayList.
- Then, the
toArray()method is called to obtain an array representation of the ArrayList's elements, which is printed usingArrays.toString(arr).
Example 2 : Converting an ArrayList to a Typed Array using toArray(T[]) Method in Java
This Java program demonstrates how to convert an ArrayList of integers into an array of the integers using the toArray(T[]) method. This method is particularly useful when we want to specify the type of the array ensuring the type safety and avoiding the need for the type casting.
import java.util.*;
// Driver Class
public class GFG {
// main function
public static void main(String[] args)
{
// create object of ArrayList
ArrayList<Integer> ArrLis
= new ArrayList<Integer>();
// Add elements
ArrLis.add(32);
ArrLis.add(67);
ArrLis.add(98);
ArrLis.add(100);
// print ArrayList
System.out.println(" ArrayList: " + ArrLis);
// Get the array of the elements
// of the ArrayList
// using toArray(T[]) method
Integer arr[] = new Integer[ArrLis.size()];
arr = ArrLis.toArray(arr);
System.out.println(" Elements of ArrayList "
+ "as Array: "
+ Arrays.toString(arr));
}
}
Output
ArrayList: [32, 67, 98, 100] Elements of ArrayList as Array: [32, 67, 98, 100]
Explanation of the Program:
- This Java program showcases the
ArrayListmethodtoArray(T[]), which converts anArrayListto an array of specified typeT. - First, integers are added to
ArrLis. - Then, an
Integerarray of the same size is initialized and passed totoArray(arr)to obtain the array representation ofArrLis, printed usingArrays.toString(arr).