withIndex

Returns an Iterator that wraps each element produced by the original iterator into an IndexedValue containing the index of that element and the element itself.

Since Kotlin

1.1

Samples

import java.util.*

fun main() { 
   //sampleStart 
   val iterator = ('a'..'c').iterator()

for ((index, value) in iterator.withIndex()) {
    println("The element at $index is $value")
} 
   //sampleEnd
}

Returns an Iterator that wraps each element produced by the original iterator into an IndexedValue containing the index of that element and the element itself.

Since Kotlin

1.0

Samples

import java.util.*

fun main() { 
   //sampleStart 
   val iterator = ('a'..'c').iterator()

for ((index, value) in iterator.withIndex()) {
    println("The element at $index is $value")
} 
   //sampleEnd
}

Returns a lazy Iterable that wraps each element of the original array into an IndexedValue containing the index of that element and the element itself.

Since Kotlin

1.0

Returns a lazy Iterable that wraps each element of the original collection into an IndexedValue containing the index of that element and the element itself.

Since Kotlin

1.0