1. List and MutableList: what they are and how they differ
If an array in Kotlin is a “fixed-size box” (like a warehouse container: however many slots you were given is how many you have), then List is a “list” that can grow and shrink while the program runs. But the main thing that makes List a list is order: elements are stored in a specific sequence, and each element has a position (an index). Kotlin explicitly describes List as a collection with ordered storage and index-based access.
At the same time, it’s important to remember a nuance: List is a read-only interface. That means through a reference of type List you can read, but cannot modify the list. If you need to add, remove, or replace elements, you use MutableList (it extends the list’s capabilities with write operations).
A small reference table:
| Type | Can read by index | Can add/remove | Typical way to create |
|---|---|---|---|
|
yes | no | |
|
yes | yes | |
2. Indices and element access
Order and indices: why the first element is 0
When you work with a list, you almost always think “first”, “second”, “last”. The computer thinks “0th”, “1st”, “2nd”… and so on. Indices in Kotlin start at zero and go up to lastIndex, where lastIndex = size - 1. This is not a “Kotlin quirk”, but a common style in most languages.
It helps to keep a short diagram in mind:
size = 4
indices: 0 1 2 3
^ ^
first lastIndex
And this is where the classic beginner mistake is born: trying to access list[size] as the “last element”. Unfortunately, size is the number of elements, not the last index. The last index is size - 1 or lastIndex.
Mini example:
fun main() {
val xs = listOf(10, 20, 30, 40)
println(xs.size) // 4
println(xs.lastIndex) // 3
}
Reading elements: list[i] and get(i)
Once you accept the philosophy that “indices start at zero”, the next step is learning how to retrieve an element. In Kotlin there are two forms that do the same thing: list[i] and list.get(i). The first reads more simply; the second is sometimes useful when you want to emphasize that this is a method call.
Example:
fun main() {
val words = listOf("one", "two", "three", "four")
println(words[0]) // one
println(words.get(2)) // three
println(words[3]) // four
}
If the index is invalid (for example, -1 or size), the program will crash with an out-of-bounds error. That’s normal: the list protects itself from “grab me an element from a parallel universe”.
3. Basic operations of MutableList
Replacing an element by index
When you move from List to MutableList, the magic begins: we can change elements. The simplest operation is replacing the value at an existing position. The syntax looks like you’re working with an array: list[i] = newValue.
Internally, this is equivalent to set(i, newValue), but the [] form reads more friendly.
Example:
fun main() {
val items = mutableListOf("a", "b", "c")
items[1] = "B" // replaced the element with index 1
println(items) // [a, B, c]
}
The important meaning here: we replaced the element at position 1. We did not “insert” anything, the size did not change, and other elements did not shift.
Adding elements: add(value)
Now let’s talk about growing a list. In real tasks, you almost always start with an empty list and fill it gradually: the user enters tasks, items go into a cart, expense records, participant names—whatever.
MutableList.add(value) adds an element to the end of the list. This is one of the most common operations, and it’s easy to think of as “append to the end”.
Example:
fun main() {
val numbers = mutableListOf(1, 2, 3)
numbers.add(4)
numbers.add(5)
println(numbers) // [1, 2, 3, 4, 5]
println(numbers.size) // 5
}
If you feel a little joy when size grows, congratulations: you just experienced your “first container growth”. An array can’t do that.
Removing by index: removeAt(index) and the shifting effect
Removal is where indices start behaving like indices. After you remove one element, all elements to the right “slide left”, and their indices change. This is logical: if one person leaves a line, everyone behind them moves closer to the front.
removeAt(index) removes the element at position index and returns the removed element. This is handy: you can immediately print what exactly was removed or use the value further.
Example:
fun main() {
val items = mutableListOf("x", "y", "z")
val removed = items.removeAt(0)
println(removed) // x
println(items) // [y, z]
}
A visual shift diagram:
was: [ x, y, z ]
indices 0 1 2
removeAt(0)
now: [ y, z ]
indices 0 1
If you store “important numbers” of elements (for example, the user selected “delete item #2”), then after deletion the numbers of the remaining elements change. That’s not a bug; it’s math.
Removing by value: remove(value)
MutableList has another way to remove: remove(value). Unlike removeAt, it works not with a position but with the value itself: “remove the "bread" element”.
Two important ideas start here.
First: remove(value) returns a Boolean. true if the removal happened, false if that value didn’t exist (meaning the list didn’t change).
Second: if there are multiple equal values, remove(value) removes the first matching occurrence, not “all at once”.
Example:
fun main() {
val products = mutableListOf("milk", "bread", "tea", "bread")
val ok1 = products.remove("bread")
println(ok1) // true
println(products) // [milk, tea, bread]
val ok2 = products.remove("coffee")
println(ok2) // false
println(products) // [milk, tea, bread]
}
Notice: "bread" appeared twice, and after removal one remained. That’s normal—we removed only the first match.
Checking membership: contains(value) and the in operator
When you work with lists, you often need to ask, “Is this already there?” For example: “Is this item already in the cart?”, “Is this command supported?”, “Has this task already been added?”.
You can write list.contains(value), or you can write a more readable version— value in list. It’s the same meaning; the second option is closer to human language.
Mini example:
fun main() {
val allowed = listOf("add", "list", "remove")
println(allowed.contains("list")) // true
println("add" in allowed) // true
println("exit" in allowed) // false
}
A list can contain duplicates (unlike a Set), and contains / in only tell you “there is at least one such element or not”. Kotlin emphasizes that List allows duplicates and keeps elements in order.
4. Practice: “Shopping list v0”
Now we’ll build a small fragment of an app that will grow along with the course. For now, without complex commands and without architecture: we’ll just show how MutableList helps store data, and how user input can add and remove items.
We’ll keep the model as simple as possible: a list of strings MutableList<String>, where each string is a product.
Printing the list “in a human-friendly way”
Let’s start with a print function. We already know loops, so we’ll just iterate over the products and show them with numbers. Notice: the number for a human starts at 1, but the index in the list starts at 0. Therefore, “humanNumber = index + 1”.
fun printShoppingList(items: List<String>) {
println("Shopping list:")
for (i in 0..items.lastIndex) {
println("${i + 1}. ${items[i]}")
}
}
If the list is empty, 0..items.lastIndex will look strange (for example, 0..-1). To avoid surprises, we’ll add a simple guard.
fun printShoppingList(items: List<String>) {
if (items.isEmpty()) {
println("Shopping list is empty.") // Shopping list is empty.
return
}
println("Shopping list:")
for (i in 0..items.lastIndex) {
println("${i + 1}. ${items[i]}")
}
}
Adding an item: add(...) and trim()
Now let’s write a function that reads a line, trims whitespace, and adds it to the list. Here we use the familiar pattern “read → prepare → use”.
fun addItem(items: MutableList<String>) {
print("Enter an item: ")
val raw = readln()
val name = raw.trim()
if (name.isEmpty()) {
println("Empty name — not adding.") // Empty name — not adding.
return
}
items.add(name)
println("Added: $name") // for example: Added: milk
}
Removing by number: removeAt(index) and converting “number → index”
Now let’s implement removal by the number the user enters (1, 2, 3, …). We need to convert the number to an index: index = number - 1. And we must check bounds; otherwise, hello, runtime error.
fun removeItemByNumber(items: MutableList<String>) {
print("Enter the item number to remove: ")
val number = readln().trim().toIntOrNull()
if (number == null) {
println("That is not a number.") // That is not a number.
return
}
val index = number - 1
if (index < 0 || index > items.lastIndex) {
println("No item with number $number.") // No item with number 10.
return
}
val removed = items.removeAt(index)
println("Removed: $removed") // for example: Removed: milk
}
Mini-main: just to feel that the list is alive
For now, we’re not building a full command menu (it easily turns into a “tiny operating system”). Instead, we’ll do 3 steps: add two items, print, remove by number, print again.
fun main() {
val items = mutableListOf<String>()
addItem(items)
addItem(items)
printShoppingList(items)
removeItemByNumber(items)
printShoppingList(items)
}
If you run this and try some input, you’ll feel the key point: MutableList is a container that’s convenient to modify, and the add/removeAt operations do exactly what it says on the box.
5. Useful nuances and a cheat sheet
val for MutableList is normal
At this stage, many people make a logical (and very human) mistake: “If the list changes, then the variable must be var.” In reality, most of the time the variable should be val, because you’re not going to reassign the reference to a different list—you’re going to change the contents of the current list.
An example of “proper normality”:
fun main() {
val items = mutableListOf("a", "b")
items.add("c")
items[0] = "A"
println(items) // [A, b, c]
}
val prevents doing this:
items = mutableListOf("x") // not allowed, but we usually don't need it anyway
But it doesn’t stop you from calling add, removeAt, remove, or changing elements. It’s like: “the house address is fixed, but you can rearrange the furniture inside”.
Cheat sheet table for MutableList operations
Sometimes it’s useful to have a compact cheat sheet so you don’t confuse methods (especially remove and removeAt—they love sneaking up at night and swapping meanings).
| Operation | Example | What it does | What it returns |
|---|---|---|---|
| Read | |
Gets an element by index | |
| Replace | |
Replaces an element by index | |
| Add | |
Adds to the end | |
| Remove by index | |
Removes the element at position |
removed |
| Remove by value | |
Removes the first match of |
|
| Check membership | |
Whether there is at least one |
|
6. Common mistakes when working with List and MutableList
Mistake #1: confusing size and lastIndex.
It’s very tempting to write list[list.size] and expect “the last element”, because “size must mean the end”. But size is the number of elements, and indices go from 0 to size - 1 (that is, lastIndex). Avoiding the mistake is simple: “the last element” is list[list.lastIndex], not list[list.size].
Mistake #2: trying to modify a List created with listOf.
listOf(...) gives a read-only List. It’s great for a “list of constants” or a “temporary set of values”, but if you want to add/remove later, you need mutableListOf(...) and the MutableList type.
Mistake #3: mixing up removeAt(index) and remove(value).
This is a classic “grabbed the wrong key” situation. removeAt(2) removes the element at position 2, while remove("2") (if you wrote it like that) would try to remove the value "2". A helpful habit: if the name contains At, it means “by location”, i.e., by index.
Mistake #4: forgetting that after removeAt elements shift.
Beginners sometimes store an index, remove an element, and then try to access the “next” element using the old index, and get surprised by the behavior. After removal, the list changes: all elements on the right “move” to a smaller index, and that’s normal. If you write code that removes multiple elements in a row, you must remember this shift and proceed carefully.
Mistake #5: thinking that remove(value) will remove all matches.
When there are duplicates in the list, remove(value) removes only the first match. That matches the meaning of a list: order matters, and duplicates are allowed. If you expected “remove all”, you expected a different operation (but we’re not introducing it today—first we’ll learn to confidently control the basic mechanics).
Mistake #6: writing var items = mutableListOf(...) “because the list changes”.
Usually val is better: you change the contents, not switch the variable to a new list. var is needed if you truly plan to replace the entire list. The discipline of val by default makes code calmer: fewer places where “suddenly everything got reassigned”, and fewer chances to lose data.
GO TO FULL VERSION