List vs MutableList
Part of the Fundamentals section of Coddy's Kotlin journey. Lesson 67 of 93.
So far, you've worked with individual variables that hold single values. But what if you need to store a collection of items, like a list of names or a series of numbers? That's where lists come in.
Kotlin provides two types of lists: List and MutableList. The key difference is whether you can modify the list after creating it.
A List is read-only. Its interface does not expose operations to add, remove, or replace elements. It is not a guarantee of deep immutability: the same underlying mutable object could change through another reference:
val fruits = listOf("Apple", "Banana", "Cherry")
val numbers = listOf(1, 2, 3, 4, 5)A MutableList allows modifications. You can add new elements, remove existing ones, or update values:
val fruits = mutableListOf("Apple", "Banana")
fruits.add("Cherry") // Now contains: Apple, Banana, CherryUse listOf() when your data shouldn't change, and mutableListOf() when you need flexibility. This distinction helps prevent accidental modifications and makes your code's intent clearer.
Challenge
MediumCreate a read-only list called colors containing three strings: "Red", "Green", and "Blue".
Then create a mutable list called numbers starting with two integers: 10 and 20.
Add the value 30 to the numbers list.
Finally, print both lists on separate lines in this order:
- The
colorslist - The
numberslist
Try it yourself
fun main() {
// TODO: Create a read-only list called 'colors' with "Red", "Green", "Blue"
// TODO: Create a mutable list called 'numbers' with 10 and 20
// TODO: Add 30 to the numbers list
// TODO: Print both lists on separate lines (colors first, then numbers)
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsModulo OperatorAugmented AssignmentComparison OperatorsRecap - Simple Math7Basic IO
Println FunctionString TemplatesReadLine InputType ConversionRecap - Years Until RetirementRecap - True or False10Functions
Declare A FunctionParameters And ArgumentsReturn ValuesNamed ArgumentsDefault ValuesSingle Expression FunctionsRecap - Sigma FunctionRecap - Validation Function2Variables
Val vs VarType InferenceNumbersStringBooleanNaming ConventionsRecap - Initialize Variables5Operators Part 2
Logical Operators Part 1Logical Operators Part 2Logical Operators Part 3Ternary With If ExpressionRecap - Simple Logic8Bill Split Calculator
Welcome MessageGetting Input3Nullability
What Is Null SafetyNullable TypesSafe Call OperatorElvis OperatorFunction Challenge BasicsNot Null AssertionRecap - Safe Access6Decision Making
If StatementIf - ElseIf As An ExpressionWhen ExpressionWhen With RangesRecap - Simple Calculator9Loops
For LoopWhile LoopDo-While LoopBreakContinueRanges In LoopsNested LoopRecap - FactorialRecap - Dynamic Input12Lists Basics
List vs MutableListAccessing ElementsModifying ListsList MethodsPair And TripleRecap - Product ListRecap - Reversed ListPractice on your own: Kotlin playground