Friday, August 28, 2009

How to convert List to Array using Java

List is interface and ArrayList is class which implements List Interface.
To add elements to List, add() method is used.

List provied toArray metiond which is used to convert basically Collection to Array using Java
List.toArray(new String[0]):

Java Program to convert List to Array :

List lstFruit = new ArrayList();
lstFruit.add("Apple Juice "); 
lstFruit.add("Pinaple Juice"); 
lstFruit.add("Watermalones Juice"); 

String[] arrayFruits = lstFruit.toArray(new String[0]);        
for(int i = 0; i < arrayFruits.length; ++i){
   System.out.println(arrayFruits[i]);
} 


Output:
Apple Juice
Pinaple Juice
Watermalones Juice


Author : Anuj Patel
Blog : http://goldenpackagebyanuj.blogspot.in/

No comments:

Post a Comment