0Pricing
Android Academy · Lesson

Lists and Mutable Lists

Store ordered data.

Lists and Mutable Lists is a free Android Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Android Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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}")
}

Accessing Items by Index

Read an item using square brackets and its index. The first item is at index 0.

You can also use first() and last() for the ends of the list.

fun main() {
    val colors = listOf("red", "green", "blue")
    println(colors[0])
    println(colors.first())
    println(colors.last())
}

Looping Over a List

The for loop visits each item in order. This is the most common way to process every element.

Here we print each name on its own line.

fun main() {
    val names = listOf("Ada", "Linus", "Grace")
    for (name in names) {
        println("Hello, $name")
    }
}

Creating a Mutable List

When you need to change the contents, use mutableListOf(). It lets you add and remove items after creation.

Use add() to append an item to the end.

fun main() {
    val tasks = mutableListOf("wake up", "code")
    tasks.add("sleep")
    println(tasks)
}

Removing Items

Use remove() to delete a specific value, or removeAt() to delete by index.

Both only work on mutable lists. Calling them on a read-only list is a compile error.

fun main() {
    val nums = mutableListOf(10, 20, 30, 40)
    nums.remove(20)
    nums.removeAt(0)
    println(nums)
}

Updating an Item

On a mutable list, assign a new value to an index to replace the existing item.

The index must already exist, otherwise you get an out-of-bounds error.

fun main() {
    val scores = mutableListOf(5, 8, 3)
    scores[1] = 99
    println(scores)
}

Checking Contents

Use contains() or the in keyword to check if a list holds a value.

Use isEmpty() to test whether a list has no items. These work on both list types.

fun main() {
    val pets = listOf("cat", "dog")
    println("cat" in pets)
    println(pets.contains("fish"))
    println(pets.isEmpty())
}

Read-Only vs Mutable Types

The type List<T> is read-only. The type MutableList<T> adds change operations.

A common pattern is to build a list with a mutable type, then expose it as a read-only List so other code cannot modify it.

val safe: List<Int> = mutableListOf(1, 2, 3)
// safe.add(4) would NOT compile

Size and Indices

The size property tells you how many items a list holds.

Valid indices run from 0 to size - 1. Use indices to loop over positions.

fun main() {
    val items = listOf("a", "b", "c")
    for (i in items.indices) {
        println("$i -> ${items[i]}")
    }
}

Adding Many at Once

Use addAll() to append every item from another collection to a mutable list.

This is handy when merging results, such as combining two pages of data.

fun main() {
    val list = mutableListOf(1, 2)
    list.addAll(listOf(3, 4, 5))
    println(list)
    println("Total: ${list.size}")
}

Quick Check

Test your understanding of Kotlin lists.

Recap

You learned that lists are ordered and indexed from 0. listOf() makes read-only lists; mutableListOf() makes changeable ones.

You can add, remove, update, loop, and check contents. Prefer read-only lists unless you truly need to mutate.

Frequently asked questions

Is the “Lists and Mutable Lists” lesson free?

Yes — the full text of “Lists and Mutable Lists” is free to read here on the web, and the Android Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Android Academy course, upgrade to CoddyKit PRO.

What will I learn in “Lists and Mutable Lists”?

Store ordered data. You practise Android Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Android Academy?

No prior experience is required. Android Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Lists and Mutable Lists” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Android Academy lesson?

Yes. Every Android Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Lists and Mutable Lists
  2. Maps and Sets
  3. map, filter, forEach
  4. Lambdas and Higher-Order Functions
← Back to Android Academy