Home Java Beginners Array to Collection
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Array to Collection

Advertisement
In this example we are converting values of an array into collection.

Array to Collection

     

In this example we are converting values of an array into collection.

List  interface is a member of the Java Collection Framework and extends Collection interface. List interface is an ordered collection which follows insertion order, typically allow duplicate elements. The List interface provides a special iterator, called ListIterator for insertion, replacement and  bi-directional access. 
The Arrays class contains various methods for manipulating arrays (e.g. sorting and searching etc.) and extends the Object class. The Iterator class allows access to the elements of a collection.

Methods used:
hasNext():
This method is used to get more elements. If the element at the next position is found then returns true otherwise throws an exception.

next(): This method returns the next element in the iteration.

asList(Object[] a): This method acts as a bridge between array and collection. This method converts array into Arrays by internally using  Colletion.toArray() method .

The code of the program is given below:

import java.util.Arrays;
import java.util.List;
import java.util.Iterator;
 
public class ArraysToCollection{
  public static void main(String[] args){
  String[] array = {"Java""Struts""JSP""J2EE"};
  List list = Arrays.asList(array);
  Iterator iterator = list.iterator();
  System.out.println("\nThe values into Array");
  while (iterator.hasNext()){
   System.out.println((Stringiterator.next());
 }
 }
}

The output of the program is given below:

C:\rajesh\kodejava>javac ArraysToCollection.java
C:\rajesh\kodejava>java ArraysToCollection
The values into Array
Java
Struts
JSP
J2EE

Download this example.

Advertisement

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: December 19, 2008

Ask Questions?    Discuss: Array to Collection   View All Comments

Post your Comment


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