Lists

Lists

To store many objects of the same type, we can use a list:

>>> val L = listOf("CS109", "is", "the", "best")
>>> L
[CS109, is, the, best]
>>> L.size
4
>>> L[0]
CS109
>>> L[3]
best

The type of L is not just List, but List<String>. List is a parameterized type, where the type parameter indicates the type of the objects stored inside the list. Type parameters are given using angular brackets.

Like in Python, the elements of the list can be accessed as L[i] for an index i. The first element has index 0, the last one has index L.lastIndex. The length of the list can be obtained as L.size (and so L.lastIndex == L.size - 1.

You can also use a for-loop to look at the elements of an array one-by-one:

>>> for (e in L)
...   println(e)
CS109
is
the
best

This is called "iterating over the list and is quite similar to the for-loop in Python.

Often we need the indices of the elements while we iterate over this list. This can be done like this:

>>> for (i in L.indices)
...   println("$i:${L[i]}")
0: CS109
1: is
2: the
3: best


There are many other useful methods of lists:

• A.size is the length of the list;
• A.lastIndex is the last valid index (that is A.size - 1);
• A.isEmpty() is the same as A.size == 0;
• A.isNotEmpty() is the same as A.size != 0;
• A.first() the first element of the sequence;
• A.last() the last element of the sequence;
• x in A or A.contains(x) tests if the list contains an element equal to x;
• A.take(n) returns a new list that contains the first n elements of A;
• A.takeLast(n) returns a new list that contains the last n elements of A;
• A.drop(n) returns a new list that has the elements of A except for the first n elements;
• A.dropLast(n) returns a new list that has the elements of A except for the last n elements;
• A.max(), A.min(), A.sum() return the largest, smallest, and sum of elements in the list;
• A.reversed() returns a new list with the elements of A in reverse order;
• A.sorted() returns a new list with the elements of A in sorted order;
• A.joinToString() returns a string with all elements of A concatenated together (see below for options);
• A + B returns a new list that contains the elements of lists A and B concatenated;
• A + el returns a new list that contains the elements of A with the element el appended at the back;
• A.indexOf(el) and A.lastIndexOf(el) return the first and last index containing an element equal to x, or -1 if no such index exists;

The joinToString method has a number of options, which are best set as named parameters, for instance:

>>> val a = listOf(1, 2, 3, 4, 5)
>>> a.joinToString()
1, 2, 3, 4, 5
>>> a.joinToString(prefix="{", postfix="}")
{1, 2, 3, 4, 5}
>>> a.joinToString(separator="/")
1/2/3/4/5
>>> a.joinToString(separator="-", prefix="<", postfix=">")
<1-2-3-4-5>


Mutable lists

So far we didn't discuss any way you can change a list—and in fact, you cannot! A List cannot be changed after it was created.

If you want to modify the sequence of elements, like replacing elements, adding elements, or removing elements, you need to use a MutableList.

Again, you can create a mutable list from a given set of elements:

>>> val a = mutableListOf("CS109", "is", "the", "best")
>>> a
[CS109, is, the, best]

If you want to start with an empty list, you need to indicate the type of elements explicitely:
>>> val b = mutableListOf<String>()
>>> b
[]

We can then add elements (of the right type) at the end:
>>> a.add("of")
true
true
>>> a
[CS109, is, the, best, of, all]

We can also add elements at the front or in the middle of the list, by providing an index for adding:
>>> a.add(0, "Course")
>>> a
[Course, CS109, is, the, best, of, all]
>>> a
[Course, CS109, is, the, best, course, of, all]


Elements are modified by simply assigning to the index:

>>> a[1] = "CS206"
>>> a
[Course, CS206, is, the, best, course, of, all]


And elements can be removed from the list using removeAt:

>>> a.removeAt(3)
the
>>> a
[Course, CS206, is, best, course, of, all]


A few more useful methods of mutable lists are:

• a.clear() removes all elements from the list;
• a.addAll(b) adds all elements of list b to the back of list a;
• a.addAll(i, b) adds all elements of list b at index i into list a;
• a.remove(el) removes an element equal to el from the list, if it exists;
• a.reverse() reverses the order of elements in the list;
• a.sort() sorts the elements of the list in increasing order;
• a.sortDescending() sorts the elements of the list in decreasing order.
 Lists