1.1-Introductie-Programmeren

Theory Lists 2

Competence: I can use arrays alongside ArrayLists and understand the difference between the two types.

Specific learning goals:

The assignments can be found in the separate modules.

Summary

Arrays are the “standard” (basic) implementation for lists in most programming languages. An ArrayList uses (as the name suggests) an array to store data. The reason we use an ArrayList in this module to create our lists instead of an array is mainly for convenience. There are some important differences between an ArrayList and an array:

Arrays

You have secretly been working quite a lot with arrays lately. You just (probably) didn’t know it yet! ArrayLists use arrays, as the name suggests.

In Java, an array is the “standard” implementation of a list and works (just like the ArrayList) on the basis of indices. The big difference between the two list types, however, is the use.

Differences with ArrayList

The biggest difference between arrays and ArrayLists is the fact that arrays have a fixed size. This means that if you create an array of size 5, it can only have 5 elements. If you remove an element from an array, you create a “gap” that you need to take into account in your program. An ArrayList is an extension to the array and solves some of these problems for you automatically.

An ArrayList contains an array internally and is designed to solve any problems directly. For example, the ArrayList will automatically create a new array for you if the first one becomes too small and copy the contents from the old to the new array. Also, the ArrayList will automatically make sure that elements that are left in a list after an element is removed or added are put back to a correct index. If you were using an array rather than an ArrayList, you would have had to do all these things yourself.

The fact that an array has a fixed size is both an advantage and a disadvantage. If you know in advance how big a list is going to be (such as a top 10), an array is more efficient to use. If you want to force a total of 10 elements in a list, an array can do this for you automatically. After all, an ArrayList will always be able to grow.

The disadvantage of an array is its lack of ease of use: you have to do the administration yourself (keep track of holes, make sure there is enough room, etc.), so you don’t have any “tools” that can make your life easy. For this module you will therefore often use the ArrayList instead of an array, but since the array occurs so often in programming, it is important that you have seen it once!

Syntax

There are two ways to create a new array. You can create a new (empty) array of a certain size or fill them with values immediately.

// Approach 1 - Direct initialisation of values:
char[] letters = {'a', 'b', 'c', 'd', 'e', 'f' };

// Approach 2 - Leave holes for later:
int[] numbers = new int[6];

Please note the use of the brackets ([]). This is how you create an array of any type. Basically, you can easily create an array of all variables by adding these two brackets to the type. Another example: Person somePerson refers to a single person and Person[] persons suddenly refers to a list of Person instances!

The indexing of an array then goes in the same way as an ArrayList. The first element gets index 0, the second element gets index 1, etc.

Adding an element to an array is the same as assigning a variable a value:

char[] letters = new char[3];

letters[0] = 'a';
letters[1] = 'b';
letters[2] = 'c';

int[] numbers = new int[3];
numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;

If you want to retrieve an element from an array, you do so in the following way:

char someLetter = letters[0]; // Haal de waarde op van index 0 uit de array numbers, dit bevat het character 'a'

int someNumber = numbers[6]; // ERROR! Index 6 bestaat niet!

Finally, you can ask, for example, what the size of an array is. Below is an example that also illustrates the difference with an ArrayList.

int[] arrayNumbers = {1, 2, 3, 4, 5};

ArrayList<Integer> listNumbers = new ArrayList<>();
for(int i = 1; i <= 5; i++) {
    listNumbers.add(i);
}

int arraySize = arrayNumbers.length; // Returns 5
int listSize = listNumbers.size(); // Also returns 5

Note the difference between the two! With an array you use a variable length to retrieve the size. With an ArrayList you use the method size().

To make the use of arrays a bit easier, Java has created a whole box of tools that you can use. https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/Arrays.html