A brief recap of arrays

An array is an object that contains a limited number of places for values.

The length (or size) of an array is the number of places in it; in other words, how many values can be stored in the array.

The size of an array is always predetermined: it is chosen when the array is created, and cannot be changed later.

The array type is defined with square brackets preceded by the type of the elements in the array (typeOfElements[]).

An array is created with the new call, followed by the type of the elements in that array, square brackets, and the number of elements in the array places inside the square brackets.

int[] numbers = new int[3];
String[] strings = new String[5];

The elements of the array are referred to by the indexes.

Setting a single value to a certain position is done similarly to setting a value to a regular variable, just that when placing the value in an array, you use the index to indicate the position.

To discover the size of an array you can use the public object variable length that arrays have.

Arrays can be used in the exact same manner as other variables, so they can be object variables, method parameters, return values of methods, and so on.

A significant portion of generally used data structures use arrays in their internal implementation.

Lists

Creating a new list

The array is created as type object, and changed to type generic with (A[]) new Object[10]; — this is done because Java does not support the call new A[10]; for now.

public class List<Type> {
    private Type[] values;

    public List() {
        this.values = (Type[]) new Object[10];
    }
}

In the beginning, every element in the array contains a null-reference.

Adding values to the list

We have to add an int variable to keep track of the first empty index in the array.

public class List<Type> {

    private Type[] values;
    private int firstFreeIndex;

    public List() {
        this.values = (Type[]) new Object[10];
        this.firstFreeIndex = 0;
    }

    public void add(Type value) {
        this.values[this.firstFreeIndex] = value;
        this.firstFreeIndex++; // same as this.firstFreeIndex = this.firstFreeIndex + 1;
    }
}