In this tutorial, you will learn how to convert ArrayList to Array in Java. We will see two ways to do the conversion: In the first program, we will do the conversion without using any method. In the second program, we will use toArray()
method of ArrayList class to do the conversion.
Example 1: ArrayList to Array conversion without using toArray() method
Steps followed in this program are:
1. Create an array with the same size as the arraylist. The arraylist size can be found using size()
method of ArrayList class. The statement String arr[] = new String[arrList.size()];
creates an array arr
of string type with the same size as of arrList
.
2. Run a for loop from 0 till the arraylist size and at every iteration get arraylist element using get() method and assign it to the array arr
.
3. Once all elements are added to the array, print array elements using enhanced for loop.
import java.util.*; public class JavaExample { public static void main(String[] args) { //ArrayList declaration and initialization ArrayList<String> arrList= new ArrayList<>(); arrList.add("String1"); arrList.add("String2"); arrList.add("String3"); arrList.add("String4"); //ArrayList to Array Conversion String arr[] = new String[arrList.size()]; for(int j =0;j<arrList.size();j++){ arr[j] = arrList.get(j); } System.out.println("Original ArrayList: "+ arrList); System.out.println("Array Elements:"); //Displaying Array elements for(String k: arr) { System.out.println(k); } } }
Output:
Example 2: ArrayList to Array conversion using toArray() method
In this example, we are using a predefined method toArray()
of ArrayList class. This method converts an ArrayList and returns an equivalent array. This is simple compared to the example 1, as here we do not have to write any logic and simply call the method toArray() to get an equivalent array.
Here we have an arraylist names
, this list contains names. We are converting this arraylist to an array using toArray()
.
import java.util.*; public class JavaExample { public static void main(String[] args) { //An ArrayList of strings ArrayList<String> names= new ArrayList<>(); names.add("Ankur"); names.add("Ajeet"); names.add("Harsh"); names.add("John"); // ArrayList to Array Conversion using toArray() String arr[]=names.toArray(new String[names.size()]); //Print array elements for(String str: arr) { System.out.println(str); } } }
Output:
Ankur Ajeet Harsh John
Example 3: ArrayList of Integer type to an int array
In this example, we have an array of integer type. This array contains integers as elements. We are converting this arraylist to an int array using the toArray()
method.
import java.util.*; public class JavaExample { public static void main(String[] args) { //An ArrayList of integers ArrayList<Integer> numbers= new ArrayList<>(); numbers.add(11); numbers.add(22); numbers.add(33); numbers.add(44); numbers.add(55); // ArrayList to Array Conversion using toArray() Object arr[]=numbers.toArray(); //Print array elements for(Object num : arr) { System.out.println(num); } } }
Output:
11 22 33 44 55
Leave a Reply