0
0
Kotlinprogramming~10 mins

List creation (listOf, mutableListOf) in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - List creation (listOf, mutableListOf)
Start
Choose list type
listOf
Create immutable list
Return list object
Use list in code
End
You decide which list to create: an immutable list with listOf or a mutable list with mutableListOf. Then Kotlin creates the list and returns it for use.
Execution Sample
Kotlin
val fruits = listOf("Apple", "Banana", "Cherry")
val numbers = mutableListOf(1, 2, 3)
numbers.add(4)
println(fruits)
println(numbers)
Creates an immutable list of fruits and a mutable list of numbers, adds a number to the mutable list, then prints both lists.
Execution Table
StepActionList CreatedList TypeList ContentOutput
1Call listOf("Apple", "Banana", "Cherry")fruitsImmutable["Apple", "Banana", "Cherry"]
2Call mutableListOf(1, 2, 3)numbersMutable[1, 2, 3]
3Add 4 to numbers listnumbersMutable[1, 2, 3, 4]
4Print fruits listfruitsImmutable["Apple", "Banana", "Cherry"][Apple, Banana, Cherry]
5Print numbers listnumbersMutable[1, 2, 3, 4][1, 2, 3, 4]
6End of programProgram ends
💡 Program ends after printing both lists.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
fruitsundefined["Apple", "Banana", "Cherry"]["Apple", "Banana", "Cherry"]["Apple", "Banana", "Cherry"]["Apple", "Banana", "Cherry"]
numbersundefinedundefined[1, 2, 3][1, 2, 3, 4][1, 2, 3, 4]
Key Moments - 2 Insights
Why can't we add an element to the list created with listOf?
The list created with listOf is immutable, meaning it cannot be changed after creation. See execution_table step 1 where fruits list is created immutable, and step 3 where only the mutable list numbers is changed.
What happens when we call mutableListOf?
mutableListOf creates a list that can be changed. In execution_table step 2, numbers list is created mutable, allowing step 3 to add an element.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the content of 'numbers' after step 3?
A[1, 2, 3]
B[1, 2, 3, 4]
C[4]
D[]
💡 Hint
Check the 'List Content' column for 'numbers' at step 3 in execution_table.
At which step is the immutable list 'fruits' created?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Action' column in execution_table for when listOf is called.
If we tried to add an element to 'fruits', what would happen?
AThe element is added successfully.
BThe list silently ignores the addition.
CA compile-time error occurs.
DThe program crashes at runtime.
💡 Hint
Recall that 'fruits' is immutable as shown in variable_tracker and key_moments.
Concept Snapshot
listOf(...) creates an immutable list that cannot be changed.
mutableListOf(...) creates a mutable list that can be changed.
Use listOf for fixed data, mutableListOf to add or remove items.
Immutable lists prevent accidental changes.
Mutable lists allow adding, removing, or updating elements.
Full Transcript
This example shows how Kotlin creates lists using listOf and mutableListOf. First, listOf creates an immutable list called fruits with three strings. Then mutableListOf creates a mutable list called numbers with three integers. We add a number 4 to the numbers list. Finally, we print both lists. The fruits list stays the same because it is immutable. The numbers list changes because it is mutable. This teaches the difference between fixed and changeable lists in Kotlin.