ArrayLists

You may have already realised that Arrays are rather limited - we need to know what size they are before we use them, and it would not be particularly easy to go about rearranging the contents of an Array. Furtunately Java provides us with a much more flexible version of Array called ArrayList. ArrayList enables us to add (insert or append) and remove elements (at any given index) from the array. When we create an ArrayList, we would normally specify the type of data that it stored, however we cannot store primitive types in an ArrayList. Fortunately, Java provides classes to represent the types, and they can be used pretty much interchangeably with their primitive equivalent. What's more they also provide some additional methods making it easy to convert between formats. The table below shows some of the primitives type and their Class equivalent

PrimitiveClass
intInteger
doubleDouble
charCharacter
booleanBoolean

Declaring an ArrayList

The syntax for this is slighly unusual - the following code shows how to create two ArrayLists, one to hold Strings, and another to hold ints:

ArrayList<String> stringList = new ArrayList<String>();
ArrayList<Integer> intList = new ArrayList<Integer>();

In order to manipulate an ArrayList, we call methods on it (in the a similar way to how we can call methods on a String like .charAt()). They key methods are as follows:

MethodPurposeDetails
add()Adds an item to the end of the list
clear()Empties the list
contains()Determines if the list contains a specific item
get()Returns an item at the specified index in the list
indexOf()Returns the (first located) position of the specified item in the listReturns -1 if that item is not in the list
isEmpty()Checks if the list is empty
lastIndexOf()Returns the last position of the specified item in the listReturns -1 if that item is not in the list
remove()Depending on the parameter passed in removes either the item at that index, or the first occurance of that item from the listReturns true if it found (and removed) the item
set()At the specified index, replaces the item with the one passed in to the method
size()Returns the size of the list

You can view a full list of ArrayList's methods in the Java documentation

Task - Zoo names

Scenario - A local zoo names all its animals. When a new animal is born or purchased by the zoo, it is given a name. No two animals may have the same name, however, if an animal has died a new animal could assume its name.

Write a programme that will prompt the user to input names - you should use a loop, so they can keep entering names. If the user enters a unique name, it should be stored in an Array list, however if the name already exists in the list they should be advised and the name should not be stored. Use small methods, write unit tests for them, and also manually test the programme works as desired. Do not worry about storing the names after the programme is terminated at this stage.

Next modify the programme to ensure the system cannot be cheated by users entering the same name, but with different capital letters. Ensure unit tests are in place for the new functionality

Add an additional modification that enables the user to type in a name which should then be removed from the list. You should include code that ensures the item is there before you delete it

Task - sorting

Java makes it fairly simple to sort arrays containing simple types of object (e.g. Integers, Strings etc.). Amend the above programme so that users are able to chose to see a list of all names stored in the system output in alphabetical order.

Task - Puzzle

Is there any difference between the outcome of executing the two code snippets below?

ArrayList<Integer> intList = new ArrayList<Integer>();
intList.add(1);
intList.add(2);
intList.add(3);
int two = 2;
intList.remove(two);
ArrayList<Integer> intList = new ArrayList<Integer>();
intList.add(1);
intList.add(2);
intList.add(3);
Integer two = 2;
intList.remove(two);
Once you have determined the outcome (perhaps by writing the code and then checking each array's contents), ensure that you understand the reason for the results being as they are

Research

ArrayList is one form of collection available in Java. Other forms of collection are available, in particular one which name also ends in 'List'. Find out more about this type of collection, including how it differs in implementation from ArrayList.