Skip to main content
Logo image

Problem Solving with Algorithms and Data Structures using Kotlin The Interactive Edition

Section 3.19 Lists

Throughout the discussion of basic data structures, we have used Kotlin lists to implement the abstract data types presented. The list is a powerful, yet simple collection mechanism that provides the programmer with a wide variety of operations. However, not all programming languages include a list collection. In these cases, the notion of a list must be implemented by the programmer. Even in Kotlin, it is sometimes useful to use different implementations of lists than the default one. We will therefore look at some different approaches for implementing lists using more fundamental memory representations.
A list is a collection of items where each item holds a relative position with respect to the others. More specifically, we will refer to this type of list as an unordered list. We can consider the list as having a first item, a second item, a third item, and so on. We can also refer to the beginning of the list (the first item) or the end of the list (the last item). For simplicity, we will assume that lists cannot contain duplicate items.
For example, the collection of integers 54, 26, 93, 17, 77, and 31 might represent a simple unordered list of exam scores. Note that we have written them as comma-delimited values, a common way of showing the list structure. Printing the list in Kotlin would show this list as [54, 26, 93, 17, 77, 31].
We refer to this as an unordered list because we do not require that the items within be ordered in a particular way, such as in sorted order. Nonetheless, each item in an unordered list is addressable by its index in the list. In the above example, 54 is at index location 0, 26 is at index location 1, and so on. So the term "unordered" should be understood to mean that the list does not enforce a particular ordering of the items based on their values, but they still occur in a fixed order based on how they were inserted into the list.
You have attempted of activities on this page.