A for loop allows you to iterate through an entire array. Let's take a look at several ways how you can use it.
Iterating through an array
The simplest way to process each array element is to use the following template:
for (element in array) {
// body of loop
}
Suppose we have an array that includes weekdays. Let's print each day of the week:
fun main() {
val daysOfWeek = arrayOf("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 an array of integers, characters, as well as of any other type.
Iterating by indexes
It is possible to access elements by their indexes directly from the loop. To do that, you must get the array.indices property that represents a range of valid array indexes.
Take a look at the daysOfWeek array:
fun main() {
val daysOfWeek = arrayOf("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 to iterate through an array. They are useful when you need to process each array element. Sometimes, you may need to access a particular subarray. You can specify the range of the indexes you need.
The index of the first element of an array is always 0.
Take a look at the program below:
fun main() {
val daysOfWeek = arrayOf("Sun", "Mon", "Tues", "Wed", "Thur", "Fri", "Sat")
for (index in 1..5) {
println("$index: ${daysOfWeek[index]}")
}
}
It will print only the workdays using a closed-ended range index in 1..5:
1: Mon
2: Tues
3: Wed
4: Thur
5: Fri
Also, you can use open-ended range index in 1..<5, look atd this code:
fun main() {
val daysOfWeek = arrayOf("Sun", "Mon", "Tues", "Wed", "Thur", "Fri", "Sat")
for (index in 1..<5) {
println("$index: ${daysOfWeek[index]}")
}
}
1: Mon
2: Tues
3: Wed
4: Thur
To use the last index of an array in ranges, you need to access array.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 an array in reverse order, use downTo in a range. You can also specify the offset between indexes using step.
This program below will print days in reverse order with a step of 2.
fun main() {
val daysOfWeek = arrayOf("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
You can iterate through an entire array or a part of it in both direct and reverse orders with any step you need.
Reading array elements
Some tasks require you to read the array elements from the input. The first element in the input here is the array size, in other words, the number of array elements.
For example, the program below reads integer numbers and prints them in reverse order.
fun main() {
val size = readln().toInt()
val array = IntArray(size)
for (i in 0..array.lastIndex) {
array[i] = readln().toInt()
}
for (i in array.lastIndex downTo 0) {
print("${array[i]} ")
}
}
This 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 ways to iterate through arrays with a for loop in Kotlin. We can iterate through each array element, by element indexes, and by range indexes. In this light, Kotlin gives you a lot of freedom. Some applications can require reading array elements from the input. We have covered it too.