How to obtain array from ArrayList?

Sometimes you need to obtain an array from ArrayList
Because:
-array is much more faster than ArrayList.
-To pass an array to a method that is not overloaded to accept collection.
-To integrate collection based code with legacy code that does not understand collection.

Here you need the toArray() defined by Collection.
Example:
import java.util.*;
class demo
{
public static void main(String[] args){
ArrayList al=new ArrayList();
al.add("bhabani");
al.add("pinku");
al.add("mahesh");
al.add("arvind");
al.add("arjan");
String name[]=new String[al.size()];
name=al.toArray(name);
for (String i:name)
{System.out.println(i);}
}};

1 comment: