Explore Java Hacks and Tutorials
Be Social. Be Updated.

How to Convert ArrayList into Array


Here is a question about converting ArrayList to an array. We can do this using two simple methods in the java.util.ArrayList. These methods return the Object[] which can be typecasted. Here are two programs that illustrate two methods separately for a better understand. These are provided with explanation and analysis for help. Not only this you'll learn a lot of things in this post (bonus!)


import java.util.*;
class ArrayListToArray
{
    public static void main(String args[])
    {
    ArrayList<String> a=new ArrayList<String>();

    a.add("Gowtham");
    a.add("Gutha's");
    a.add("Java");
    a.add("-");
    a.add("demos");
    a.add(".");
    a.add("blogspot");
    a.add(".");
    a.add("com");

    String st[]=a.toArray(new String[a.size()]);
   
    System.out.println("The elements are");

        for(String k:st)
        System.out.println(k);
    }
}


Output of the Program


The elements are
Gowtham
Gutha's
Java
-
demos
.
blogspot
.
com

Explaining toArray(T[]) method

This method returns an array of type of the object of ArrayList elements which is also the type of this method's parameter. For example, as you can see in the above program, i have sent new String[a.size()] as parameter and the elements in the ArrayList are of type String. So as there are multiple elements of type String we'll get a String[] in return. This returned String[] contains all the elements in the ArrayList in the order. This method is created for generic types. The parameter T[] clearly expresses this.
If you try changing the type of the parameter and the return type such as specifying the type of the parameter as an Integer array and the return type as String[] you'll probably get an error even if you do vice versa.

The prototype of this method is


<T> T[] toArray(T[] array)

Note that if the type of the ArrayList is a String and you are passing and taking Integer[] to and from the method respectively, you'll not get a compile time error rather you'll get an exception stating


Exception in thread "main" java.lang.ArrayStoreException
        at java.lang.System.arraycopy(Native Method)
        at java.util.ArrayList.toArray(Unknown Source)

Here is an example illustrating toArray() method. The prototype is
Object[] toArray()


import java.util.*;
class ArrayListToArray
{
    public static void main(String args[])
    {
    ArrayList<String> a=new ArrayList<String>();

    a.add("Gowtham");
    a.add("Gutha's");
    a.add("Java");
    a.add("-");
    a.add("demos");
    a.add(".");
    a.add("blogspot");
    a.add(".");
    a.add("com");

    Object[] ob=a.toArray();
   
    System.out.println("The elements are");

        for(Object k:st)
        System.out.println(k);
    }
}


You'll get the same output for this. But when it comes to explanation, the toArray() method is a non-generic version. The return type of this method is Object[]. However, you can work out with the generic version of the ArrayList class. In this case, it is not even necessary to have ArrayList<String>, it is enough if you write ArrayList. But when you convert to array, you'll not get the generic type however. One more thing is that you cannot typecast it. This is because arrays cannot be typecasted directly like

String[] st=(String[])a.toArray();

which will lead to an error. As you will get an Object[] you cannot access String (here) methods such as substring() etc. Note that by the above statement, you'll not get a compile time error rather you'll receive a ClassCastException.

In this way, conversion of ArrayList into an array made simple. Also see Sort an Array in increasing order



Posted by at on

Tags: Collections Framework Java Utilities