What is an ArrayList?

The ArrayList class is a Java class that you can use to store lists of objects. You can also store objects in an array, but arrays have a couple of obvious problems.

The primary goal of the Java ArrayList class is to provide a class that replicates many of the features of arrays, while adding some new features that are designed to work around the problems listed above.

Basics of ArrayLists

To create an ArrayList object you use the following syntax.

ArrayList<Type> A = new ArrayList<Type>();

Here Type is the type that you are planning to store in the ArrayList. For example, to make an ArrayList that can hold Strings you would do

ArrayList<String> B = new ArrayList<String>();

A fundamental limitation of ArrayLists is that they can only hold objects, and not primitive types such as ints.

ArrayList<int> C = new ArrayList<int>();
// Illegal - int is not an object type

The workaround for this is that Java provides a class equivalent for every one of the primitive types. For example, there is an Integer class corresponding to the int type and a Double class corresponding to the double type, and so on. The first step to being able to store a list of ints in an ArrayList is to instead create an ArrayList that can hold a list of Integer objects:

ArrayList<Integer> D = new ArrayList<Integer>();
// OK - Integer is an object type

When you create an ArrayList, the list you get is initially empty. To add new items to the end of the ArrayList you use the add() method:

ArrayList<String> B = new ArrayList<String>(); // B starts out empty.
B.add("Hello"); // B now has size 1
B.add("World"); // B new has size 2

Adding items to an ArrayList of Integers is similar:

ArrayList<Integer> D = new ArrayList<Integer>();
D.add(new Integer(3)); // D now has size 1
D.add(new Integer(15)); // D now has size 2

An alternative way to add ints to an ArrayList is to use the autoboxing feature. This feature automatically converts primitive types into their object equivalents:

ArrayList<Integer> D = new ArrayList<Integer>();
D.add(3); // D now has size 1
D.add(15); // D now has size 2

To retrieve items from an ArrayList, use the get() method.

ArrayList<Integer> D = new ArrayList<Integer>();
D.add(3);
D.add(15);
int x = D.get(1); // x is now 15

To determine the size of an ArrayList, use the size() method.

ArrayList<Integer> D = new ArrayList<Integer>();
D.add(3);
D.add(15);
D.add(-46);
for(int n = 0;n < D.size();n++)
  System.out.println(D.get(n));

To replace an existing value with a new value, use the set() method:

ArrayList<Integer> D = new ArrayList<Integer>();
D.add(3); // Location 0 is 3
D.add(15);
D.add(-46);
D.set(0,22); // Location 0 is now 22

Along with being to add items and have the list resize automatically to accomodate them, you can also remove items and have the list shrink automatically each time you remove an item. To remove an item, use the remove() method with the index of the item you want to remove. For example, to remove the last item in an ArrayList you would do

D.remove(D.size() - 1);