Kotlin MutableList.set
Replace one stop at a known position while preserving the rest of the route.
A simple example
MutableList.set
- Start with A, B
- Do this Replace the second item with C
- Result A, C
Syntax
stops[index] = newStop
Run these examples in Kotlin
Save one example at a time as Lesson.kt. Use Kotlin 2.2 on JDK 21.
kotlinc Lesson.kt -include-runtime -d lesson.jar
java -jar lesson.jarCorrect the second stop on a shuttle route
A dispatcher spots an outdated address at the second stop on today's shuttle route. The position on the schedule is correct, so only the destination at that position should change.
From the programstops[1] = "Harbor"
ResultHarbor
fun main() {
val stops = mutableListOf("School", "Park")
stops[1] = "Harbor"
println(stops[1])
}
Harbor
The second stop changes from Park to Harbor.
Advertisements
Correct a lab register
A student signed the lab sheet with a nickname. The second entry must be corrected in place. Which value occupies that position after the change?
GivenAda · Kit → replace Kit with Kiran
Worked resultKiran
Change the next stop
The shuttle cannot use the quay this morning. The second entry must be corrected in place. Which value occupies that position after the change?
GivenSchool · Quay → replace Quay with Pier
Worked resultPier
What to watch for
The position must already exist. Use add to grow the route.