The ArrayList

The ArrayList is similar to the array but is more advanced and easier to use. Whereas arrays are created with a fixed size and cannot be changed, ArrayLists can expand and shrink dynamically to fit their contents. ArrayList is an instantiable class so we need to use the new keyword to create an ArrayList. As an example, the following line creates an ArrayList that can hold Strings:

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

Let’s break this down. On the right is the code new ArrayList<String>(). This creates an ArrayList in memory, which is empty by default. The ArrayList uses angle brackets (<>) for us to specify the type of data it will hold. In this case, we’re creating an ArrayList that can hold String values—it is an “ArrayList of String”. The parentheses we leave empty because there’s no data we need to pass to it. On the left is the variable that holds (a reference to) the ArrayList. I’ve named it sal, short for “String ArrayList”. The variable’s type is ArrayList<String> so that it is able to hold (a reference to) the ArrayList. If we were to print sal, this is what we’d see:

[]

An empty pair of brackets. This shows that the ArrayList sal is empty. Let’s look at a few operations that ArrayLists provide.

Adding an Element

We can add strings to sal by using the add method:

sal.add("toaster");
sal.add("microwave");
sal.add("fridge");
sal.add("oven");
sal.add("kettle");

Each line adds a String to sal, so sal now looks like this:

[toaster, microwave, fridge, oven, kettle]

Every time an element is added, it’s placed at the end of the list, which is why the elements are in the same order as they were added.

Removing an Element

To remove an element, you use the remove method. You can specify either the index or the string to be removed. For example, if you want to remove “microwave”, you can either write:

sal.remove(2);

or

sal.remove("microwave");

In either case, “microwave” would be removed. All elements to the right are shifted one to the left to close the gap. Here is what sal looks like before and after the removal:

Before: [toaster, oven, microwave, fridge, kettle]
After:  [toaster, oven, fridge, kettle]

Inserting an Element

Maybe you want to insert a string somewhere in the middle of the list instead of at the end. In that case you use the add method again but pass two arguments, the first being the index and the second being the element, for example:

sal.add(1, "freezer");

This will insert “freezer” at index 1. The element previously at index 1 and all subsequent elements are shifted to the right to make room.

Before: [toaster, oven, fridge, kettle]
After:  [toaster, freezer, oven, fridge, kettle]

Setting an Element

The set method replaces an element at the specified index, for example:

sal.set(4, "dishwasher");

Index 4 will be set to the string “dishwasher”.

Before: [toaster, freezer, oven, fridge, kettle]
After:  [toaster, freezer, oven, fridge, dishwasher]

Getting an Element

To get (read) an element from the list, you use the get method. This method takes an index number and returns the element at that index, e.g.

String s = sal.get(0);

This returns the element at index 0. Therefore, “toaster” is returned and placed in variable s. Note that the list is unaffected by this operation; get returns the value but doesn’t remove it from the list.

ArrayList Element Type

One minor downside to ArrayList is that it cannot hold primitive types, only reference types. This means that while you can make an int array:

int[] arr = new int[10];

you cannot make an int ArrayList:

ArrayList<int> list = new ArrayList<int>(); // This is an error

Fortunately, for every primitive type there is a reference type equivalent in the form of a class:

PrimitiveReference
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble
charCharacter
booleanBoolean

So, if you want an ArrayList that stores integers, you don’t make an ArrayList of int, but an ArrayList of Integer:

ArrayList<Integer> list = new ArrayList<Integer>(); // This works

Now, Integer is not the same as int but Java automatically converts between the two to make our lives easier. This is called autoboxing. This means you can do:

list.add(5);

and Java won’t complain because it will convert 5 from int to Integer before it gets added to the ArrayList.

Leave a Reply

Your email address will not be published. Required fields are marked *