Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Collection to Array 
 

In this section, you will learn to convert the collection into a array with the help of example.

 

Collection to Array

                         

The example given below illustrates the conversion of  a collection into a array. In this example we creating an object of ArrayList, adding elements into this object and storing this object into List interface. Convert elements of ArrayList into an array by using toArray().

List  interface is a member of the Java Collection Framework and extends Collection interface. It is an ordered collection which follows insertion order, typically allow duplicate elements. The class ArrayList extends AbstractList class and implements List, RandomAccess, Cloneable, Serializable interfaces. ArrayList class is a member of the Java Collections Framework.

The method used:
toArray(Object[] a): This method returns an array containing all of the elements of the given list.

The code of the program is given below:

import java.util.List;
import java.util.ArrayList;
 
public class CollectionToArray1{
    public static void main(String[] args){
        List arraylist = new ArrayList();
        arraylist.add("Java");
        arraylist.add("Struts");
        arraylist.add("J2ee");
        arraylist.add("Code");
        Object[] array = arraylist.toArray();
    System.out.println("\nThe objects into array.");
        for (int i = 0; i < array.length; i++)
        {
         System.out.println(array[i].toString());
        }
    }
}

The output of the program is given below:

C:\rajesh\kodejava>javac CollectionToArray.java
Note: CollectionToArray.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
C:\rajesh\kodejava>java CollectionToArray
The objects into array.
Java
Struts
J2ee
Code

Download this example.

                         

» View all related tutorials
Related Tags: c string ide class reference io references size sed get vi key value field fields this id length max preferences

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

2 comments so far (
post your own) View All Comments Latest 10 Comments:

How to convert Number,String and char in vector method to array ?

Posted by varun on Saturday, 01.19.08 @ 18:14pm | #45593

how to convert a number, string and char in vector method to array

Posted by varun on Saturday, 01.19.08 @ 18:12pm | #45592

Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.