Session 20: The ArrayList class

The ArrayList class
    Adding elements
    Accessing elements
    Removing elements
    Comparison with arrays
Wrapper classes (Section 13.3)
Exercise

The ArrayList class

The ArrayList class is intended for holding a sequence of elements. Actually, it bears a strong resemblance to an array, but in many cases the ArrayList class is more convenient. Namely, a ArrayList automatically grows and shrinks as you add and remove objects.

The ArrayList is a library class. (It's found in java.util, but I recommend the abridged version in csbsju.cs160 in this class.) To create one, you declare a variable of type ArrayList, and set it up to point to a new ArrayList.

ArrayList vec = new ArrayList();
When you create an ArrayList object, it initially is holding nothing.

The ArrayList class has a lot of methods. I'm not going to list them all here. The documentation is much more helpful on this point.

Adding elements

The ArrayList class provides three important methods for putting new data into the ArrayList.

void add(Object obj)
Adds obj at the list's end.

void add(int index, Object obj)
Inserts obj at index index. Objects at this index and beyond are shifted up to accommodate the new object.

Object set(int index, Object obj)
Changes the object at index index to obj. Returns the value previously at that index.

For example, the following would create an ArrayList holding strings representing the numbers from 1 to 10.
ArrayList count = new ArrayList();
for(int i = 1; i <= 10; i++) count.add("" + i);

NOTE: An ArrayList object can hold only objects. It can't hold primitive types, like int. We'll see a workaround soon - but it's simply a workaround.

Accessing elements

Three methods are particularly useful for accessing information about an ArrayList object.

int size()
Returns the number of elements in the list.

Object get(int index)
Returns the object at index index of the list.

int indexOf(Object obj)
Returns the index of obj in the list, or -1 if not found. It uses the equals() method of obj to determine if two elements are equal.

The get() method is a little irritating: The ArrayList class is written to hold a sequence of objects of any type; as far as ArrayList knows, all its elements are simply in the Object class. Thus, you cannot do the following.

String str = count.get(5);
The get() method returns an Object, and Java won't automatically cast down the inheritance hierarchy. You need to have an explicit casting operator to make this work.
String str = (String) count.get(5);

It's worth noting that indexOf() uses the equals() method to determine whether it has found a match. Thus, count.indexOf("5") returns 4, even though "4" != count.get(4), since the two objects (that happen to represent the same string) are at different locations in memory.

Removing elements

You can also remove elements from an ArrayList object. This would be the inverse of adding something into the ArrayList.

Comparison with arrays

ArrayLists are very useful. In many cases where we used an array in the past, an ArrayList would be more convenient - for example, they would be very appropriate for the Very Small Video lab (but you're not allowed to go back and use ArrayLists for it!). If the program used an ArrayList instead, then the store wouldn't have an arbitrary limit on how many videos and customers the store could have..

When should you use an array, and when should you use an ArrayList?

Wrapper classes

Textbook: Section 13.3

So what do you do when you want to store a primitive value into an ArrayList? Well, you use the wrapper classes, classes provided in the java.lang package. (Incidentally, java.lang is automatically imported in every Java program - it's the only such package in Java.)

For example, if you want to create an object to represent an int value 8, you'd create an instance of the Integer class.

Integer ival = new Integer(8);
This is something you can legitimately insert into an ArrayList.
count.add(new Integer(8));

Ok, it's there. But how do you access an int value from the ArrayList? Well, you have to remove the Integer instance and then call the intValue() method on it.

Integer out_obj = (Integer) count.get(0);
int out = out_obj.intValue();
Or, more succinctly (and confusingly):
int out = ((Integer) count.get(0)).intValue();

There are similar classes for all the primitive types: Boolean, Byte, Character, Double, Integer, Float, Long, and Short. Each has the appropriate xxxValue() method for extracting the primitive-type value from the object.

Yes, it's a pain. But it's the best way to get a primitive-type value into an ArrayList.

Exercise