Convert Collection to Array

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.

Convert Collection to 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.