Modifying Lists
Part of the Fundamentals section of Coddy's Kotlin journey. Lesson 69 of 93.
With MutableList, you can change your list after creating it. Let's explore the core operations for modifying list contents.
To add elements, use add(). You can append to the end or insert at a specific index:
val tasks = mutableListOf("Email", "Meeting")
tasks.add("Report") // Adds at the end
tasks.add(1, "Call") // Inserts at index 1
// Result: [Email, Call, Meeting, Report]To remove elements, use remove() to delete by value, or removeAt() to delete by index:
val items = mutableListOf("A", "B", "C", "D")
items.remove("B") // Removes "B"
items.removeAt(0) // Removes element at index 0
// Result: [C, D]To update an existing element, assign a new value using its index:
val colors = mutableListOf("Red", "Green", "Blue")
colors[1] = "Yellow"
// Result: [Red, Yellow, Blue]Remember, these operations only work with MutableList. Attempting them on a regular List will cause a compilation error.
Challenge
MediumCreate a mutable list called playlist with three songs: "Song A", "Song B", and "Song C".
Perform the following modifications in order:
- Add
"Song D"to the end of the list - Insert
"Song X"at index 1 - Remove
"Song B"from the list (by value) - Update the element at index 0 to
"New Song A"
Print the final list.
Try it yourself
fun main() {
// TODO: Create a mutable list called playlist with "Song A", "Song B", and "Song C"
// TODO: Add "Song D" to the end of the list
// TODO: Insert "Song X" at index 1
// TODO: Remove "Song B" from the list (by value)
// TODO: Update the element at index 0 to "New Song A"
// Print the final list
// println(playlist)
}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