withIndex

fun <T> Array<out T>.withIndex(): Iterable<IndexedValue<T>>
fun ByteArray.withIndex(): Iterable<IndexedValue<Byte>>
fun ShortArray.withIndex(): Iterable<IndexedValue<Short>>
fun IntArray.withIndex(): Iterable<IndexedValue<Int>>
fun LongArray.withIndex(): Iterable<IndexedValue<Long>>
fun FloatArray.withIndex(): Iterable<IndexedValue<Float>>
fun DoubleArray.withIndex(): Iterable<IndexedValue<Double>>
fun BooleanArray.withIndex(): Iterable<IndexedValue<Boolean>>
fun CharArray.withIndex(): Iterable<IndexedValue<Char>>

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.


fun <T> Iterable<T>.withIndex(): Iterable<IndexedValue<T>>

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.


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.3

fun <T> Iterator<T>.withIndex(): Iterator<IndexedValue<T>>

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.

Samples

import 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
}