In this post, we will see how to convert List to Array in java.
There are many ways to convert List
to Array
in java
Table of Contents
Using toArray() method
We can use toArray()
method to convert List to String. We can either pass a array of size as List’s length or we can pass empty array.
This returns an array containing all the elements of the list in correct order.
There two overloaded methods for toArray()
.
Syntax:
1 2 3 4 5 |
T[] toArray(T[] x) and public Object[] toArray(); |
Where,T represents generic.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
import java.util.List; import java.util.ArrayList; class ListToArrayToArray { public static void main(String arg[]) { //creation of list of type String List<String> list=new ArrayList<>(); list.add("Hello"); list.add("I"); list.add("Am"); list.add("John"); //print content of list System.out.println("Elements of list are:"); System.out.println(list); //create an String array of size of list //list.size() to know size of list String[] arr=new String[list.size()]; //assign all the element of list in array using toArray() method by passing array arr=list.toArray(arr); System.out.println("Elements of Array are:"); for(int i=0; i < arr.length; i++) { //printing element of array System.out.println(arr[i]); } // print elements of array using for each loop System.out.println("Display using for each loop:"); for (String a: arr) { System.out.println(a); } } } |
OUTPUT:
[Hello, I, Am, John] Elements of Array are:
Hello
I
Am
John
Please note that if you do not pass any parameter to toArray()
method, it will give us Object array.
Using Java 8’s Stream
We can simply use java 8‘ stream’s toArray()
method to convert List to array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
package org.arpit.java2blog; import java.util.ArrayList; import java.util.List; public class ListToArrayJava8Main { public static void main(String[] args) { //creation of list of type String List<String> list=new ArrayList<>(); list.add("John"); list.add("Martin"); list.add("Mary"); list.add("Andrew"); //print content of list System.out.println("Elements of list are:"); System.out.println(list); String[] arr=list.stream().toArray(String[]::new); System.out.println("Elements of Array are:"); for(int i=0; i < arr.length; i++) { //printing element of array System.out.println(arr[i]); } } } |
Output:
[John, Martin, Mary, Andrew] Elements of Array are:
John
Martin
Mary
Andrew
Using Arrays.copyOf
We can use Arrays.copyOf()
method also to convert List to Array. We need to use toArray()
method and pass it to Arrays.copyOf()
method to convert it to specified type
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
package org.arpit.java2blog; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class ListToArrayCopyOfMain { public static void main(String[] args) { //creation of list of type String List<String> list=new ArrayList<>(); list.add("John"); list.add("Martin"); list.add("Mary"); list.add("Andrew"); //print content of list System.out.println("Elements of list are:"); System.out.println(list); String[] arr=Arrays.copyOf(list.toArray(),list.size(),String[].class); System.out.println("Elements of Array are:"); for(int i=0; i < arr.length; i++) { //printing element of array System.out.println(arr[i]); } } } |
Output:
[John, Martin, Mary, Andrew] Elements of Array are:
John
Martin
Mary
Andrew
Using System.arraycopy
We can use System.arraycopy()
method which copies an specified array, starting at the specified position, to the specified position of the target array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
package org.arpit.java2blog; import java.util.ArrayList; import java.util.List; public class ListToArrayArrayCopyMain { public static void main(String[] args) { //creation of list of type String List<String> list=new ArrayList<>(); list.add("India"); list.add("China"); list.add("Nepal"); list.add("Bhutan"); //print content of list System.out.println("Elements of list are:"); System.out.println(list); //create an String array of size of list //list.size() to know size of list String[] arr=new String[list.size()]; System.arraycopy(list.toArray(), 0, arr, 0, list.size()); System.out.println("Elements of Array are:"); for(int i=0; i < arr.length; i++) { //printing element of array System.out.println(arr[i]); } } } |
Output:
[India, China, Nepal, Bhutan] Elements of Array are:
India
China
Nepal
Bhutan
Manual method
This method use loop and run it until the size of list and one by one get elements from list and add it to Array.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
package org.arpit.java2blog; import java.util.List; import java.util.ArrayList; public class ListToArrayManualMain { public static void main(String arg[]) { //create and initialization of list List<Integer> list=new ArrayList<>(); list.add(800); list.add(600); list.add(400); list.add(200); //print content of list System.out.print("Elements of list are:"); System.out.println(list); //create an integer array of size of list //list.size() to know size of list int[] ar=new int[list.size()]; System.out.println("Elements of Array are:"); //assigning of element of list one by one to array ar for(int i=0; i < list.size(); i++) { ar[i]=list.get(i); //printing element of array System.out.println(ar[i]); } } } |
OUTPUT:
800
600
400
200
That’s all about converting List to Array in java.