Java Notes: ArrayList

java.util.ArrayList allows for expandable arrays, and is the Collections replacement for the older Vector class. An ArrayList has the following advantages over an array:

Use ArrayList when there will be a large variation in the amount of data that you would put into an array.

A disadvantage of a ArrayList is that it holds only Objects and not primitive types (eg, int). To use a primitive type in an ArrayList, put it inside an object (eg, to save an integer value use the Integer class or define your own class). If you use the Integer wrapper, you will not be able to change the integer value, so it is sometimes useful to define your own class.

To Create an ArrayList

ArrayLists are implemented with an array, and when that array is full and an additional element is added, a new array must be allocated. Because it takes time to create a bigger array and copy the elements from the old array to the new array, it is a faster to create an ArrayList with a size that it will commonly be when full. Of course, if you knew the final size, you could simply use an array. However, for non-critical sections of code programmers typically don't specify an initial size.

ArrayList alist1 = new ArrayList(); // Created with default size.
ArrayList alist2 = new ArrayList(300); // Starts with 300 elements

To Add elements to the end of an ArrayList

a.add(s); // adds s to the end of the ArrayList a

To get the elements from an ArrayList

Use either a for loop with an integer index to get all the elements from an ArrayList, or go over all elements in a ArrayList using an Iterator (forward) or ListIterator (forward / backward).

This example uses an Iterator to print all elements (Strings) in an ArrayList. It uses hasNext(), which returns true if there are more elements, and next(), which returns the next element as an Object, which must be downcast to the appropriate type (until Java 1.5 additions).

for (Iterator iter = a.iterator(); iter.hasNext();) {
    System.out.println((String)(iter.next()));
}

The following is faster, but less general:

for (integer i = 0; i < a.size(); i++) {
    System.out.println((String)(iter.get(i)));
}

Sorting and Searching

Call the Collections.sort(yourArrayList) or Collections.sort(yourArrayList, yourComparator). Check out Collections for other useful utility methods.

Common ArrayList Methods

Here are some of the most useful ArrayList methods. a is an ArrayList, i is an int, obj is an Object, iter is an Iterator, liter is a ListIterator.

ResultMethod Description
 a.add(obj) adds obj to end of ArrayList a
 a.add(i, obj) Inserts obj at index i, shifting elements up as necessary.
 a.clear() removes all elements from ArrayList a
ba.contains(obj) Returns true if ArrayList a contains obj
obja.get(i) Returns the object at index i.
ia.indexOf(obj) Returns index of first occurrence of obj, or -1 if not there.
itera.iterator() Returns an Iterator for forward traversal.
ia.lastIndexOf(obj) Returns index of last occurrence of obj, or -1 if not there.
litera.listIterator() Returns a ListIterator for forward / backward / modifying traversal.
 a.remove(i) Removes the element at position i.
 a.removeRange(i, j) Removes the elements from positions i thru j.
 a.set(i,obj) Sets the element at index i to obj.
ia.size() Returns the number of elements in ArrayList a.
oarraya.toArray(Object[]) The array parameter can be any Object subclass (eg, String). This returns the values in that array (or a larger array if necessary). This is useful when you need the generality of an ArrayList for input, but need the speed of arrays when processing the data.
// Assume you put only strings into nameList
String nameArray[] = new String[nameList.size()];
nameArray = nameList.toArray(nameArray);