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:
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:
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:
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:
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:
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:
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:
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:
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:
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:
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:
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:
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:
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:
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:
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).