withIndex

Common
JVM
JS
Native
1.0
fun < T > Array < out T > . withIndex ( ) : Iterable < IndexedValue < T > >
(source)
fun ByteArray . withIndex ( ) : Iterable < IndexedValue < Byte > >
(source)
fun ShortArray . withIndex ( ) : Iterable < IndexedValue < Short > >
(source)
fun IntArray . withIndex ( ) : Iterable < IndexedValue < Int > >
(source)
fun LongArray . withIndex ( ) : Iterable < IndexedValue < Long > >
(source)
fun FloatArray . withIndex ( ) : Iterable < IndexedValue < Float > >
(source)
fun DoubleArray . withIndex ( ) : Iterable < IndexedValue < Double > >
(source)
fun BooleanArray . withIndex ( ) : Iterable < IndexedValue < Boolean > >
(source)
fun CharArray . withIndex ( ) : Iterable < IndexedValue < Char > >
(source)
@ExperimentalUnsignedTypes fun UIntArray . withIndex ( ) : Iterable < IndexedValue < UInt > >
(source)
@ExperimentalUnsignedTypes fun ULongArray . withIndex ( ) : Iterable < IndexedValue < ULong > >
(source)
@ExperimentalUnsignedTypes fun UByteArray . withIndex ( ) : Iterable < IndexedValue < UByte > >
(source)
@ExperimentalUnsignedTypes fun UShortArray . withIndex ( ) : Iterable < IndexedValue < UShort > >
(source)

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.

Common
JVM
JS
Native
1.0
fun < T > Iterable < T > . withIndex ( ) : Iterable < IndexedValue < T > >
(source)

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.

Common
JVM
JS
Native
1.0
fun < T > Iterator < T > . withIndex ( ) : Iterator < IndexedValue < T > >
(source)

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.

import java.util.*

fun main(args: Array<String>) {
//sampleStart
val iterator = ('a'..'c').iterator()

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