skip to main | skip to sidebar

Java Programs and Examples with Output

Pages

▼
 
  • RSS
  • Twitter
Tuesday, October 30, 2012

To Create a collection using the Given array

Posted by Raju Gupta at 4:00 AM – 0 comments
 
This code snippet is useful for converting when a lot of array is given and needs in collection.

import java.util.*;
import java.io.*;

public class ArrayToCollection{
   public static void main(String args[]) 
   throws IOException{
      BufferedReader in = new BufferedReader
      (new InputStreamReader(System.in));
      System.out.println("Number of elements 
      you want to add to the array: ");
      int n = Integer.parseInt(in.readLine());
      String[] name = new String[n];
      for(int i = 0; i < n; i++){
         name[i] = in.readLine();
      }
      List list = Arrays.asList(name); 
      System.out.println();
      for(String li: list){
         String str = li;
         System.out.print(str + " ");
      }
   }
}


Leave a Reply

Newer Post Older Post
Subscribe to: Post Comments ( Atom )
  • Popular
  • Recent
  • Archives
Powered by Blogger.
 
 
 
© 2011 Java Programs and Examples with Output | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger