Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Java-conversion Convert List to Array

Related Tutorials


 
 

Share on Google+Share on Google+

Convert List to Array

Advertisement
Lets see how to convert List to Array.

Convert List to Array

     

Lets see how to convert List to Array. 

Code Description:

In this program we have taken a List of type String as shown below. For this we have used Mylist.add to add the contents of the list as shown below. Then we have used a method
Mylist.toArray to convert the List to Array.

Here is the code of this program:

 

 

import java.util.*;

public class ListToArr{
 public static void main(String[] args){
  List<String> Mylist = new ArrayList<String>();
  Mylist.add("Java")
  Mylist.add("is")
  Mylist.add("a");
  Mylist.add("wonderful");
  Mylist.add("language");
  String[] s1 = Mylist.toArray(new String[0])//Collection to array 
  for(int i = 0; i < s1.length; ++i){
  String contents = s1[i];
  System.out.print(contents);
  }
  }
}

Output of the program:

C:\unique>javac ListToArr.java

C:\unique>java ListToArr
Javaisawonderfullanguage
C:\unique>

Download this example.

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: August 9, 2007

Related Tutorials

Discuss: Convert List to Array   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com