Java

Convert List to Array in Java

A list differs from an array, in the sense that it can grow or shrink in length. It can also have elements inserted within its length, and so increasing the length. It can also have its elements deleted. If the aim of the list is not to make it grow or shrink or have any special function, then the array should be used. And so, it would be necessary to convert a list to an array.

In Java, a list is not a class. It is an interface. An interface in Java is like an abstract class, but it is not meant to be subclassed. An interface is meant to have unrelated classes. The method declarations without body of the interface are defined in a class implemented from the interface. This should not be confused with instantiation. An object is instantiated from a class. A method is implemented from an abstract method, which may be a member of an interface, or an abstract class. With the interface, the “abstract” method declaration is not preceded by the reserved word, abstract. Note: a class created from an interface is said to have implemented the interface.

Among classes of list already implemented in the Java compiler, are the ArrayList, LinkedList and Vector. These three classes will be used to show how a list can be converted into an array in this article, beginning with the ArrayList.

Converting ArrayList to Array

ArrayList is in the java.util package. The ArrayList class has two methods to convert its object to an array. The methods are: toArray() and toArray(T[] a).

Object[] toArray()

With this method, Java converts the ArrayList object values into an array of objects of the class, Object. All classes are descendants of the class Object (beginning with uppercase O). The object of the class Object, has the method toString(). System.out.print() can use this method (on its own) to print the array object values as strings.

The following program illustrates this:

    import java.util.*;
    public class TheClass {
        public static void main(String[] args) {
            ArrayList<Integer> nums = new ArrayList();
            nums.add(1); nums.add(2); nums.add(3); nums.add(4); nums.add(5);
            Object[] obj = nums.toArray();
            for (int i=0; i<obj.length; i++) {
                System.out.print(obj[i]); System.out.print(' ');
            }
            System.out.println();
        }
    }

The output is:

    1 2 3 4 5

Instantiation of the ArrayList takes a reference and not a primitive type. So, “Integer” should be used there, instead of “int”.

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

With the above method, a value in the ArrayList is converted to an object before the System.out.print() expression would print it out as string. In order to have each value in ArrayList as the same type in a return array, the programmer has to use this method “<T> T[] toArray(T[] a)”. T is the type of the value in ArrayList. T is also the type of the value wanted in the return array. The syntax for the statement to do this is:

            T[] retArr = alObj.toArray(array-of-T);

where retArr is the return array and alObj is the ArrayList object. array-of-T is an array with all null values of the length required. The following main() method code illustrates the use of this method:

        public static void main(String[] args) {
            ArrayList<Integer> nums = new ArrayList<Integer>();
            nums.add(1); nums.add(2); nums.add(3); nums.add(4); nums.add(5);
            Integer[] arr = new Integer[nums.size()];
            Integer[] retArr = nums.toArray(arr);
            for (int i=0; i<retArr.length; i++) {
                System.out.print(retArr[i]); System.out.print(' ');
            }
            System.out.println();
        }

The output is:

    1 2 3 4 5

Converting LinkedList to Array

LinkedList is in the java.util package. The LinkedList class has two methods to convert its object to an array. The methods are: toArray() and toArray(T[] a).

Object[] toArray()

With this method, Java converts the LinkedList object values into an array of objects of the class, Object. All classes are descendants of the class Object (beginning with uppercase O). The object of the class Object has the method toString(). System.out.print() can use this method (on its own) to print the array object values as strings.

The following program illustrates this:

    import java.util.*;
    public class TheClass {
        public static void main(String[] args) {
            LinkedList<Integer>nums = new LinkedList<Integer>();
            nums.add(1); nums.add(2); nums.add(3); nums.add(4); nums.add(5);
            Object[] obj = nums.toArray();
            for (int i=0; i<obj.length; i++) {
                System.out.print(obj[i]); System.out.print(' ');
            }
            System.out.println();
        }
    }

The output is:

    1 2 3 4 5

Instantiation of the LinkedList takes a reference and not a primitive type. So, “Integer” should be used there, instead of “int”.

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

With the above method, a value in the LinkedList is converted to an object before the System.out.print() expression would print it out as string. In order to have each value in LinkedList as the same type in the returned array, the programmer has to use this method “<T> T[] toArray(T[] a)”. T is the type of the value in LinkedList. T is also the type of the value wanted in the return array. The syntax for the statement to do this is:

            T[] retArr = llObj.toArray(array-of-T);

where retArr is the return array and llObj is the LinkedList object. array-of-T is an array with all null values of the length required. The following main() method code, illustrates the use of this method:

        public static void main(String[] args) {
            LinkedList<Integer> nums = new LinkedList();
            nums.add(1); nums.add(2); nums.add(3); nums.add(4); nums.add(5);
            Integer[] arr = new Integer[nums.size()];
            Integer[] retArr = nums.toArray(arr);
            for (int i=0; i<retArr.length; i++) {
                System.out.print(retArr[i]); System.out.print(' ');
            }
            System.out.println();
        }

The output is:

    1 2 3 4 5

Converting Vector to Array

Vector is in the java.util package. The Vector class has two methods to convert its object to an array. The methods are: toArray() and toArray(T[] a).

Object[] toArray()

With this method, Java converts the Vector object values into an array of objects of the class, Object. All classes are descendants of the class Object (beginning with uppercase O). The object of the class Object, has the method toString(). System.out.print() can use this method (on its own) to print the array object values, as strings.

The following program illustrates this:

    import java.util.*;
    public class TheClass {
        public static void main(String[] args) {
            Vector<Integer> nums = new Vector<Integer>();
            nums.add(1); nums.add(2); nums.add(3); nums.add(4); nums.add(5);
            Object[] obj = nums.toArray();
            for (int i=0; i<obj.length; i++) {
                System.out.print(obj[i]); System.out.print(' ');
            }
            System.out.println();
        }
    }

The output is:

    1 2 3 4 5

Instantiation of the Vector, takes a reference and not a primitive type. So, “Integer” should be used there, instead of “int”.

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

With the above method, a value in the Vector is converted to an object before the System.out.print() expression would print it out as string. In order to have each value in Vector as the same type in the returned array, the programmer has to use this method “<T> T[] toArray(T[] a)”. T is the type of the value in LinkedList. T is also the type of the value wanted in the return array. The syntax for the statement to do this is:

            T[] retArr = vObj.toArray(array-of-T);

where retArr is the return array and vObj is the Vector object. array-of-T is an array with all null values of the length required. The following main() method code, illustrates the use of this method:

        public static void main(String[] args) {
            Vector<nteger> nums = new Vector<Integer>();
            nums.add(1); nums.add(2); nums.add(3); nums.add(4); nums.add(5);
            Integer[] arr = new Integer[nums.size()];
            Integer[] retArr = nums.toArray(arr);
            for (int i=0; i<retArr.length; i++) {
                System.out.print(retArr[i]); System.out.print(' ');
            }
            System.out.println();
        }

The output is:

    1 2 3 4 5

Conclusion

In Java, a list is not a class. It is an interface. Among classes of list already implemented in the Java compiler, are the ArrayList, LinkedList and Vector. Each of these classes has the methods, toArray() and toArray(array). In conversion, if the aim is to print the values of the return array, use toArray(). If the aim is to have an array of the same type of the values in the list, use toArray(array).

About the author

Chrysanthus Forcha

Discoverer of mathematics Integration from First Principles and related series. Master’s Degree in Technical Education, specializing in Electronics and Computer Software. BSc Electronics. I also have knowledge and experience at the Master’s level in Computing and Telecommunications. Out of 20,000 writers, I was the 37th best writer at devarticles.com. I have been working in these fields for more than 10 years.