Learn
Lists
Introduction to Lists

In this section, we’re going to explore the various types of collections that exist in Kotlin. A collection is a group of elements that exist in a single structure. The first collection type we’ll learn about is one that’s most commonly used in Kotlin and many other programming languages: a list.

A Kotlin list is very much similar to a real-world list: it represents an ordered collection of elements that can contain duplicates. These elements can be of any primitive data type and represent a myriad of real-world scenarios. These can include student grades, restaurant ratings, a to-do list of tasks, and more.

Take a look at the image on the right. After a long day of sailing through the creek, Codey is going to set up camp and rest for the night. Being the environmentally friendly Kotlin developer that they are, they’ve opted to make a virtual to-do list of things they need to get done before nightfall:

var toDo = listOf("gather wood", "start a fire", "set up tent", "eat dinner", "stargaze")

Printing the list would result in the following output:

[gather wood, start a fire, set up tent, eat dinner, stargaze]

Notice how the syntax of a list in its definition is different from its output. If Kotlin isn’t your first programming language, you might recognize the output syntax with square brackets to be identical to that of an array.

As opposed to lists, arrays in Kotlin have a fixed size, thus not being the most convenient or easily applicable data structure. For that reason, we’ll be focusing our time on exploring lists, but we encourage you to read up on arrays in Kotlin’s documentation.

Instructions

Click Next to learn more about lists.

Folder Icon

Take this course for free

Already have an account?