Last Updated : 17 Jul, 2024
The toArray() method of ArrayList is used to return an array containing all the elements in ArrayList in the correct order.
Declaring toArray() methodpublic Object[] toArray()
or
public <T> T[] toArray(T[] a)
Parameters: This method either accepts no parameters or it takes an array T[] as a parameter which is the array into which the elements of the list are to be stored if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.
Return Value: The function returns an array containing all the elements in this list.
Exception: The first overload of this method throws no exceptions. However, the second overload throws the following exceptions:
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.
Java
// Java Program to illustrate the
// ArrayList toArray() method in Java
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));
}
}
ArrayList: [32, 67, 98, 100] Elements of ArrayList as Array: [32, 67, 98, 100]Explanation of the Program:
ArrayList
's toArray()
method to convert its elements into a regular array. toArray()
method is called to obtain an array representation of the ArrayList's elements, which is printed using Arrays.toString(arr)
.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.
Java
// Java Program to illustrate the
// ArrayList toArray(T[]) method in Java
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));
}
}
ArrayList: [32, 67, 98, 100] Elements of ArrayList as Array: [32, 67, 98, 100]Explanation of the Program:
ArrayList
method toArray(T[])
, which converts an ArrayList
to an array of specified type T
. ArrLis
. Integer
array of the same size is initialized and passed to toArray(arr)
to obtain the array representation of ArrLis
, printed using Arrays.toString(arr)
.RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4