1.1-Introductie-Programmeren

Theory Lists 1

Competence: I can use ArrayLists to store several related values.

Specific learning goals:

The assignments can be found in the separate modules.

Summary

An ArrayList is a list of values of a certain type. An ArrayList can have almost any type, for example ArrayLists of Strings, ArrayLists of integers and ArrayLists of characters. But one ArrayList can only hold the values of one type.

A value in the ArrayList is called an element. The number of elements in the list does not matter. It can be many or few, and the size of the list can change during execution of the program. A program can be written so that the list always works.

Elements can be accessed with an index number. The first element has index number 0.

Regardless of the value type, an ArrayList has a number of useful methods for you to use and change the list.

With an ArrayList you have a flexible list of many variables available under one name.

An example

In this example we will make a list of names using an ArrayList.

// declare and initialize a new arraylist, capable of holding Strings
ArrayList<String> names = new ArrayList<String>();

We add a few names:

// add a few names to the list
names.add("Kees");
names.add("Piet");
names.add("Truus");

Now we’re curious how many names are on the list:

// how many names do we have?
SaxionApp.printLine("The list contains " + names.size() + " names");

And is Karel also on the list?

// test if a name is on the list
if (names.contains("Karel")) {
    SaxionApp.printLine("The name Karel is on the list");
} else {
    SaxionApp.printLine("Karel is not on the list);
}

Finally we don’t want Truus on this list.

// remove a name
names.remove("Truus");

ArrayList, elements and index numbers

In an ArrayList you can store a practically unlimited amount of values, as long as these values are of the same type. An ArrayList itself is a variable with a variable name. The values IN the ArrayList are called the elements of the ArrayList. The elements can be reached with an index number.

Note! the first element has index number 0 . Example

With an ArrayList you therefore have several elements at your disposal under one variable name. With an Arraylist you can write a program in which the list becomes longer or shorter and you can perform all operations regardless of the length of the list.

ArrayList is more than a list

Each Arraylist variable has a number of methods that you can use to use and change the list. These methods are listed in the Java documentation of ArrayLists. Below are some examples:

Add an element to the list with the method add();

studentNames.add("Peter");

The argument of the add() method is the value that will be stored as a new element at the end of the list. The type of this argument depends on the type of ArrayList. In the example above studentName is an ArrayList of Strings. In the following example we see an ArrayList of integers:

groupSizes.add(24);

Querying the number of elements in the ArrayList with the size() method:

studentNames.size();

The size() method has no argument and returns an integer value.

Getting the value of an element with the get() method; studentNames.get(178); The argument of the get() method is an integer and it is the index number of the element you are requesting. The return value is the value of that particular element, so its type depends on the type of the ArrayList. The example above will return a String. The following example will return an integer: groupSizes.get(3);

Note! With the index number 2 the third element is requested

There are many more methods available for an ArrayList, which you can find in the javadoc.

Declare ArrayList

An ArrayList is declared with the java keyword new. This picture compares the declaration of an ArrayList with that of a simple variable: Example

Between the angle brackets <> the type is given of the elements in the ArrayList to be created. These are written full-out with a capital first character. See the examples below:

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

ArrayList<Double> temperatures = new ArrayList<Double>();

ArrayList<Integer> customerIDs = new ArrayList<Integer>();

ArrayList<Boolean> atHomeList = new ArrayList<Boolean>();

ArrayList<Character> groupCodes = new ArrayList<Character>();

Java also allows to leave out the type in the second set of brackets:

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

A limitation when using ArrayList is that it is not allowed to create ArraLists of the so called primitive types. This means that you can’t make ArrayLists of int, double, boolean, char etc.! Why this is the case, you don’t have to know for now. Instead of using int, double, etc., you have to use the so called “Boxed” variants of these types. These are written with a capital and with the full name:

So you can’t write

ArrayList<int> numbers = new ArrayList<>();

Instead use

ArrayList<Integer> numbers = new ArrayList<>();

Extra functies

// Quickly add a few elements when making a new list with Arrays.asList()
ArrayList<Integers> numbers = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6));

// Replace the value at position 0 with a new value.
numbers.set(0, 5);