Introduction
Sometimes you need to work with a standard Java array instead of an ArrayList
. The `toArray()` method provides a simple and efficient way to convert your ArrayList
into an array of the same type.
Syntax
public Object[] toArray()
public <T> T[] toArray(T[] a)
Parameters Table
Parameter | Description |
---|---|
a (for toArray(T[] a) ) |
An array of the desired type. If the array is smaller than the ArrayList's size, it will be truncated; otherwise, it will be filled with null values. This serves as an initial array for the conversion. |
Return Value
The toArray()
method returns a new array containing all of the elements in the ArrayList
in the order they are returned by the iterator.
Examples
Example 1: Converting to an Object Array
This example demonstrates how to convert an ArrayList
of String
objects into a standard Object[]
array. This is useful when you need a generic array format.
import java.util.ArrayList;
public class ToArrayExample1 {
public static void main(String[] args) {
ArrayList<String> names = new ArrayList<>();
names.add("Alice");
names.add("Bob");
names.add("Charlie");
Object[] nameArray = names.toArray();
for (Object name : nameArray) {
System.out.println(name);
}
}
}
Alice
Bob
Charlie
Explanation: The toArray()
method without any arguments converts the ArrayList<String> names
into an Object[] nameArray
. The loop then iterates through the array and prints each element.
Example 2: Converting to a String Array
This example shows how to convert an ArrayList
of String
objects directly into a String[]
array using the type-safe overload of toArray()
. This avoids casting and is generally preferred.
import java.util.ArrayList;
public class ToArrayExample2 {
public static void main(String[] args) {
ArrayList<String> names = new ArrayList<>();
names.add("Alice");
names.add("Bob");
names.add("Charlie");
String[] nameArray = names.toArray(new String[0]);
for (String name : nameArray) {
System.out.println(name);
}
}
}
Alice
Bob
Charlie
Explanation: The toArray(new String[0])
method converts the ArrayList<String> names
into a String[] nameArray
. Note that we pass an empty String
array as an argument. This allows the ArrayList to create a new array of the correct size, avoiding potential resizing issues.
Example 3: Using with a Pre-existing Array
This example demonstrates how to use an existing array and have it populated by the ArrayList
’s elements. If the provided array is smaller than the ArrayList's size, only the first few elements of the ArrayList are copied.
import java.util.ArrayList;
public class ToArrayExample3 {
public static void main(String[] args) {
ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(1);
numbers.add(2);
numbers.add(3);
numbers.add(4);
Integer[] existingArray = new Integer[2]; // Smaller array
Integer[] result = numbers.toArray(existingArray);
for (Integer number : result) {
System.out.println(number);
}
}
}
1
2
Explanation: The existingArray
is provided as an argument to the toArray(existingArray)
method. Since it's smaller than the ArrayList
, only the first two elements (1 and 2) are copied into it. Notice that existingArray now contains [1, 2]. The returned array will be a new array containing the first two elements of numbers.
Example 4: Using with a Pre-existing Array - Larger than ArrayList
This example shows what happens when you pass an array that is larger than the list. The excess elements are filled with null values.
import java.util.ArrayList;
public class ToArrayExample4 {
public static void main(String[] args) {
ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(1);
numbers.add(2);
Integer[] existingArray = new Integer[5]; // Larger array
Integer[] result = numbers.toArray(existingArray);
for (Integer number : result) {
System.out.println(number == null ? "null" : number);
}
}
}
1
2
null
null
null
Explanation: The existingArray
is provided as an argument to the toArray(existingArray)
method. Since it's larger than the ArrayList
, only the first two elements (1 and 2) are copied into it. The remaining slots in existingArray are filled with null values.