Sometimes, we may need to convert a List to an Array in Java for various reasons. This can be done easily by using the toArray()
method provided by the List interface. In this blog post, we will explore how to convert a List to an Array in Java.
Table of Contents
Converting a List to an Array using toArray()
Method
The toArray()
method is a built-in method in Java that is used to convert a List to an Array. This method returns an Array containing all elements of the List in the same order.
Here’s how you can use the toArray()
method to convert a List to an Array:
- Create a List of elements.
List<String> fruitsList = new ArrayList<>();
fruitsList.add("Apple");
fruitsList.add("Banana");
fruitsList.add("Orange");
- Convert the List to an Array using the
toArray()
method.
String[] fruitsArray = fruitsList.toArray(new String[fruitsList.size()]);
In the above code, we pass a new Array of the same type and size as the List to the toArray()
method. The elements of the List are then copied to the Array.
Example Code
Here’s a complete example code that demonstrates how to convert a List to an Array in Java:
import java.util.ArrayList;
import java.util.List;
public class ListToArrayExample {
public static void main(String[] args) {
List<String> fruitsList = new ArrayList<>();
fruitsList.add("Apple");
fruitsList.add("Banana");
fruitsList.add("Orange");
String[] fruitsArray = fruitsList.toArray(new String[fruitsList.size()]);
// Printing the elements of the array
for (String fruit : fruitsArray) {
System.out.println(fruit);
}
}
}
Output:
Apple
Banana
Orange
In the above example, we create a List of fruits, convert it to an Array using the toArray()
method, and then print the elements of the Array.
Conclusion
Converting a List to an Array in Java can be easily done using the toArray()
method provided by the List interface. By following the steps mentioned in this blog post, you can convert a List to an Array in your Java programs.