
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert List to Array in Java
A List is a collection in Java that is used to store a sequence of elements. It is part of the Java Collections Framework and allows us to perform various operations on the elements, such as adding, removing, and accessing them.
Converting a list to an array in Java can be done using several methods. Below are some of the common ways to achieve this:
Using a For Loop
We will use the for loop to iterate through the list and copy each element into an array. This method allows us to control the size of the array and ensures that we only copy the elements we need.
Example
In the below example, we will create a list of integers and then convert it to an array using the for loop.
import java.util.ArrayList; import java.util.List; public class ListToArray { public static void main(String[] args) { List<Integer> list = new ArrayList<>(); list.add(1); list.add(2); list.add(3); list.add(4); Integer[] array = new Integer[list.size()]; for (int i = 0; i < list.size(); i++) { array[i] = list.get(i); } System.out.println("Array: "); for (Integer element : array) { System.out.print(element + " "); } } }
Using toArray() Method
The List object provides a method known as toArray(). This method accepts an empty array as an argument, converts the current list to an array, and places it in the given array. To convert a List object to an array.
Steps to Convert List to Array
- Create a List object.
- Add elements to it.
- Create an empty array with the size of the created ArrayList.
- Convert the list to an array using the toArray() method, bypassing the above-created array as an argument to it.
- Print the contents of the array.
Example
In the below example, we will create a list of integers and then convert it to an array using the toArray() method.
import java.util.ArrayList; import java.util.List; public class ListToArray { public static void main(String[] args) { List<Integer> list = new ArrayList<>(); list.add(1); list.add(2); list.add(3); list.add(4); Integer[] array = new Integer[list.size()]; array = list.toArray(array); System.out.println("Array: "); for (Integer element : array) { System.out.print(element + " "); } } }
Output
Following is the output of the above code:
Array: 1 2 3 4
Using Stream API
Similar to the toArray() method, we can also use the Stream API to convert a list to an array. The Stream API provides an easier and more readable way to perform this operation.
Example
In the following example, we will create a list of integers and then use the Stream API to convert it to an array.
import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; public class ListToArray { public static void main(String[] args) { List<Integer> list = new ArrayList<>(); list.add(5); list.add(6); list.add(7); list.add(8); Integer[] array = list.stream().toArray(Integer[]::new); System.out.println("Array: "); for (Integer element : array) { System.out.print(element + " "); } } }
Output
Following is the output of the above code:
Array: 5 6 7 8