How To Convert ArrayList into Array in Java


In This Tutorial We will Learn How To Convert ArrayList Into Array In java. We can do this by calling toArray() method .in ArrayList class We have toArray() method ,by using this method we can convert ArrayList into Array in java.

Object[] toArray() method Returns an array containing all of the elements in this ArrayList in proper sequences.

code:


package com.practice;

import java.util.ArrayList;

public class ConvertArrayListToArray {

public static void main(String[] args) {
ArrayList<Integer> arraylist = new ArrayList<Integer>();
arraylist.add(10);
arraylist.add(20);
arraylist.add(30);
arraylist.add(40);
System.out.println("ArrayList Before conversion:"+arraylist);

Integer intArray[] = new Integer[arraylist.size()];

intArray = arraylist.toArray(intArray);

for(int x :intArray){
System.out.println(x);
}

}

}

output:


ArrayList Before conversion:[10, 20, 30, 40]
10
20
30
40


No comments:

Post a Comment