Lists and Mutable Lists
Store ordered data.
What Is a List?
A list is an ordered collection of items. Each item has a position, called an index, starting at 0.
In Kotlin, lists come in two flavors: read-only lists you cannot change, and mutable lists you can add to or remove from.
Lists are everywhere in Android apps: a feed of posts, a row of buttons, search results.
Creating a Read-Only List
Use listOf() to create a read-only list. Once created, you cannot add or remove items.
This is the safest default. If your data never changes, prefer a read-only list.
fun main() {
val fruits = listOf("apple", "banana", "cherry")
println(fruits)
println("Size: ${fruits.size}")
}All lessons in this course
- Lists and Mutable Lists
- Maps and Sets
- map, filter, forEach
- Lambdas and Higher-Order Functions