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



Home Java Java-conversion Convert Collection to Array

Related Tutorials


 
 

Share on Google+Share on Google+

Convert Collection to Array

Advertisement
In this section, you will learn to convert Collection data to an Array.

Convert Collection to Array

     

In this section, you will learn to convert Collection data to an Array. 

Code Description:

This program helps you in converting Collection data to an Array. Here we have taken an collection of weekdays which gets converted to Array using days.toArray method. 

 

 

import java.util.*;
import java.io.*;
import java.util.ArrayList;
import java.util.Collection;

public class CollToArr {
  public static void main(String args[]) {
  String weekdays[] "Sunday""Monday""Tuesday""Wednesday",
 
"Thursday","Friday""Saturday"};
  Collection<String> days = new ArrayList<String>();
  for (int i = 0, n = weekdays.length; i < n; i++) {
  days.add(weekdays[i]);
  }
  String s[] (String[])days.toArray(new String[0]);
  for (int i = 0, n = s.length; i < n; i++) {
  System.out.println(s[i]);
  }
  }
}

Output of the program: 

C:\unique>javac CollToArr.java

C:\unique>java CollToArr
Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday

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 6, 2007

Related Tutorials

Discuss: Convert Collection to Array   View All Comments

Post your Comment


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