How do I convert collection into array?

Category: java.util, viewed: 28723 time(s).

To convert collection-based data into array we can use toArray() method provided by the implementation of Collection interface such as java.util.ArrayList.

import java.util.List;
import java.util.ArrayList;

public class CollectionToArrayExample
{
    public static void main(String[] args)
    {
        List list = new ArrayList();
        list.add("Kode");
        list.add("Java");
        list.add("Sample");
        list.add("Code");

        Object[] array = list.toArray();
        for (int i = 0; i < array.length; i++)
        {
            System.out.println(array[i].toString());
        }
    }
}

Our sample code result is shown below:

Kode
Java
Sample
Code
Click here to lend your support to: Kode Java Org and make a donation at www.pledgie.com !

 

Can't find what you are looking for? Join our FORUMS and ask some questions!
Download Hundreds of Complimentary Industry Resources

Get hundreds of popular Industry magazines, white papers, webinars, podcasts, and more; all available at no cost to you. With more than 600 complimentary offers, you'll find plenty of titles to suit your professional interests and needs. Click Here and Sign up today!

Sponsored Links

Our Friends

Statistics

Locations of visitors to this page
visitor stats