Kotlin MutableList.addAll
Insert a group of new stops before a known position on a route.
A simple example
MutableList.addAll
- Start with A, C
- Do this Insert B between them
- Result A, B, C
Syntax
stops.addAll(index, additions)
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.jarInsert two pickup stops before the depot
A shuttle normally travels straight from the school to the depot. The dispatcher adds two afternoon pickups and needs them to appear in the supplied order before the final stop.
fun main() {
val stops = mutableListOf("School", "Depot")
stops.addAll(1, listOf("Hall", "Pool"))
println(stops.joinToString(", "))
}
School, Hall, Pool, Depot
Hall and Pool are inserted between the school and the depot in pickup order.
Extend a reading list
A class has chosen one novel and adds two more titles. The desk adds the new entries after the existing ones. Read the resulting sequence in its recorded order.
Update an equipment checklist
A technician adds the final item after packing two pieces of equipment. The desk adds the new entries after the existing ones. Read the resulting sequence in its recorded order.
What to watch for
Use a MutableList for insertion. A read-only List does not offer addAll.