I want to start a For loop from a given index in Java you can easily write
for (int i = startingIndex; i < items.size(); i++)
how to to do that in Kotlin? I know how to write a for loop in Kotlin
my example
I want to iterate over an array of strings but the start position is 3, not iterating over a Range
the iteration will be over a collection of items
Generally, the for loop is used to iterate through the given block of code for the specified number of times. In Kotlin, the for loop works like the forEach in C#. The for loop in Kotlin can be used to iterate through anything that provides an iterator. For example, a range, array, string, etc.
For iterating from the start item till the last, you can use something like this:
for (i in startingIndex until items.size) {
//apply your logic
}
Another option is to drop first n
elements and use forEach
from there:
val l = listOf(1, 2, 3, 4)
l.drop(1).forEach { println(it) } // prints "2, 3, 4"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With