In Kotlin, the for loop is a handy tool that allows you to iterate through an entire mutable list. Let's take a look at several ways of using it.
Iterating through a MutableList
The simplest way to process each mutable list element is to use the following template:
for (element in mutList) {
// body of loop
}
Suppose we have a mutable list that includes weekdays. Let's print each day of the week:
fun main() {
val daysOfWeek = mutableListOf("Sun", "Mon", "Tues", "Wed", "Thur", "Fri", "Sat")
for (day in daysOfWeek){
println(day)
}
}
After that, the program will print the following:
Sun
Mon
Tues
Wed
Thur
Fri
Sat
In the same way, you can process a mutable list of integers, characters, or any other data type.
Iterating by indexes
It is possible to access elements by their index directly from the loop. To do that, you must use the mutList.indices property, which represents a range of valid mutList indexes.
Take a look at the daysOfWeek mutable list:
fun main() {
val daysOfWeek = mutableListOf("Sun", "Mon", "Tues", "Wed", "Thur", "Fri", "Sat")
for (index in daysOfWeek.indices){
println("$index: ${daysOfWeek[index]}")
}
}
The program will print the following:
0: Sun
1: Mon
2: Tues
3: Wed
4: Thur
5: Fri
6: SatIterating by range indexes
We have discussed two ways of iterating through a mutable list. They are useful when you need to process each list element. Sometimes, however, you may need to access a particular sublist. In such a case, you can specify the range of the indexes you need.
The first element of a mutable list always has index 0.
Take a look at the program below:
fun main() {
val daysOfWeek = mutableListOf("Sun", "Mon", "Tues", "Wed", "Thur", "Fri", "Sat")
for (index in 1..5) {
println("$index: ${daysOfWeek[index]}")
}
}
It will print only the workdays:
1: Mon
2: Tues
3: Wed
4: Thur
5: Fri
To use the last index of a mutable list in ranges, you need to access mutList.lastIndex. So, we can modify the code this way:
for (index in 1 until daysOfWeek.lastIndex) {
println("$index: ${daysOfWeek[index]}")
}
It displays the same days as before:
1: Mon
2: Tues
3: Wed
4: Thur
5: Fri
If you want to iterate through a mutable list in reverse order, use downTo in a range. You can also specify the offset between indexes using step.
The program below will print days in reverse order with a step of 2:
fun main() {
val daysOfWeek = mutableListOf("Sun", "Mon", "Tues", "Wed", "Thur", "Fri", "Sat")
for (index in daysOfWeek.lastIndex downTo 0 step 2) {
println("$index: ${daysOfWeek[index]}")
}
}
Output:
6: Sat
4: Thur
2: Tues
0: Sun
So, you can iterate through an entire mutable list or a part of it in both direct and reverse order with any step you need.
Reading MutableList elements
Some tasks require you to read mutable list elements from the input.
For example, the program below reads integer numbers and prints them in reverse order.
fun main() {
val size = readln().toInt()
val mutList: MutableList<Int> = mutableListOf()
for (i in 0 until size) {
mutList.add(readln().toInt())
}
for (i in mutList.lastIndex downTo 0) {
print("${mutList[i]} ")
}
}
Here is the input:
5
1
2
3
4
5
The program will output the following:
5 4 3 2 1
You can use this program as a template for your own solutions.
Conclusion
In this topic, we have discussed various ways to iterate through mutable lists with a for loop in Kotlin: iterating through each list element, iterating by element indexes, and iterating by range indexes. In this light, Kotlin gives you a lot of freedom. Some applications can require reading mutable list elements from the input. Now you know how to do it, too. Ready to practice?