Thursday, 16 April 2015

Filled Under: ,

How To Convert Array List To String Array

When working with array list sometimes we need to obtain data in string,int and in other data types for calculation and string processing but array list contains data in object not in int or string data types.so if want to obtain data in other types so first we need to convert array list data to desired data type in this program we have converted array list in a string array.In this program first we have created a of object type to store data temporary then we use toString method to convert it into a string then we initialize it into string array.


CODING:

>
import java.util.*;
class arraylist
{
public static void main(String args[])
{
ArrayList a1 = new ArrayList();
System.out.println("Initial size of a1 = "+a1.size());
a1.add("This");
a1.add("is");
a1.add("String");
Object Obj[]=a1.toArray();
String Str[]=new String[Obj.length];
for (int i=0;i<Str.length;i++)
{
Str[i]=Obj[i].toString();
System.out.println(Str[i]);
}
}
}

OUTPUT:




0 comments:

Post a Comment