Goals

  • Understanding ArrayLists
    • Declaring ArrayLists
    • Initializing ArrayLists
    • Adding Elements
    • Getting Elements

Resources

http://processing.org/learning/topics/arraylistclass.html

"A list is only as strong as its weakest link." ~ Donald Knuth

Declaring an ArrayList

An ArrayList is an object like an array. We need to declare a reference variable that will store a reference to our ArrayList object.

ArrayList lottoNumbers;
Declares a reference variable named lottoNumbers of the type ArrayList.

Now that we have a reference variable of the type ArrayList, we can go ahead and create a new ArrayList object and assign it to this variable.

Initializing an ArrayList

We need to create a new ArrayList object and assign it to our reference variable in order to initialize the variable. Essentially, we need our reference variable to point to some ArrayList object instead of pointing at nothing.

lottoNumbers = new ArrayList();
Creates a new ArrayList object and assigns it to the reference variabel lottoNumbers.
ArrayList lottoNumbers = new ArrayList();
Optionally, like our primitive variables or arrays, we can do this in one line.

Adding Elements to an ArrayList

Notice the ArrayList did not specify how many, or what type of data it will hold. This is because an ArrayList is both size and type agnostic, meaning that it doesn't care. These are some of the great advantages an ArrayList has over an array. However with more advantages comes some additional work.

lottoNumbers.add(2);
This will add the literal value of 2 to the last position in the ArrayList.
Since the ArrayList is empty, the value 2 is only element in the list.
lottoNumbers.add(4);
This will add the literal value of 4 to the last position in the ArrayList.
Now if 4 was added to the last position in the ArrayList, what position is 2 in?
lottoNumbers.add(8); lottoNumbers -> [ 2, 4, 8 ]
lottoNumbers.add(16); lottoNumbers -> [ 2, 4, 8, 16 ]
lottoNumbers.add(32); lottoNumbers -> [ 2, 4, 8, 16, 32 ]

The only way to add elements to an ArrayList is at the back, or last position. This requires some preparation in a program if the order of elements is important.

The ArrayList add method will always add elements to the end of the ArrayList.

Getting Elements from an ArrayList

Remember how ArrayLists are type agnostic? Well if the ArrayList doesn't care what types of data it contains, how would we go about using that data. Let's take a look at how we get elements out of an ArrayList.

ArrayList stuff = new ArrayList();
stuff.add("one");
stuff.add("two");
stuff.add(3);
stuff.add(4);
So now the ArrayList has four elements, two string literals and two integers.

Let's assume we wanted test the strings for equality. We need some way of getting objects out of the ArrayList and preserving their type. Let's take a look at how this is done.

String firstElement = (String) stuff.get(0);
This will get the first item that was added to the ArrayList and cast it as a String, then assign it to the String variable named firstElement.
String secondElement = (String) stuff.get(1);
Here we've done the same thing with the second element in the ArrayList.
firstElement.equals(secondElement); false
Now that we safely extracted our String elements, cast them as Strings, and assigned them to some variables, we can use the methods of the String datatype to check if they are equal.

There is a large problem though. Say we tried to compare the elements at positions 2 and 3. These are not String literals, they are integers, so our casting to a string will not work. It is this reason that using ArrayLists requires some extra planning on the part of the programmer. You should know what types you have put in each list and how to treat them safely when getting elements back out.

ArrayLists are type agnostic, they are containers without a label.
When getting elements out of an ArrayList you must always cast the element.
Plan ahead what types get added to the ArrayList and how they will be retrieved.