ArrayList - Converting ArrayList to String Array

// ArrayList
// ArrayList to String array

/*
Write a java program to convert ArrayList to String array
*/

import java.util.*;

class ALtoSA
{
     public static void main(String as[])
     {
          // Creating ArrayList
          ArrayList <String>al = new ArrayList<String>();
          Scanner s = new Scanner(System.in);
         
          System.out.print("Enter number of strings : ");
          int N = s.nextInt();
         
          for(int i=0;i<N;i++)
          {
              System.out.print("Enter string : "); 
              String str = s.next();
              al.add(str);
          }
         
          System.out.println("Elements in ArrayList are : "+al);      
         
          // Creating a String array
          String[] SA = new String[al.size()];
         
          // Converting ArrayList to String Array
          SA = al.toArray(SA);
         
          System.out.println("Elements in String array : ");
          for(String str:SA)
              System.out.println(str);
     }
}

No comments:

Post a Comment

Don't be a silent reader...
Leave your comments...

Anu