Search⌘ K
AI Features

Lists

Explore Kotlin lists by learning to create, access, and modify both read-only and mutable lists. Understand immutability principles, element indexing, and how to manipulate list contents using Kotlin's collections framework.

Collections are a central feature of any programming language. They allow storing multiple values – such as your list of favorite books – in one convenient data structure which can then be stored and accessed with a single variable.

The fundamental collection types you’ll master in the following lessons are:

  • Lists
  • Sets
  • Arrays*
  • Maps

*Arrays are not part of the actual Collections API but are included here because they’re also a data structure for storing multiple elements.

Creating a List

Kotlin provides three convenient functions to initialize lists:

Kotlin
val siUnits = listOf("s", "m", "kg", "A", "K", "mol", "cd") // Creates readonly list
val quarks = mutableListOf("up", "down", "charm", "strange", "top", "bottom") // Creates mutable list
val physicists = arrayListOf("Albert Einstein", "Isaac Newton") // Creates mutable list

All three functions allow you to add any number of elements (try it out!). The main difference between them is that listOf creates a read-only list whereas mutableListOf creates a ...