W3cubDocs

/Kotlin

withIndex

Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)
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>>
@ExperimentalUnsignedTypes fun UIntArray.withIndex(): Iterable<IndexedValue<UInt>>
@ExperimentalUnsignedTypes fun ULongArray.withIndex(): Iterable<IndexedValue<ULong>>
@ExperimentalUnsignedTypes fun UByteArray.withIndex(): Iterable<IndexedValue<UByte>>
@ExperimentalUnsignedTypes fun UShortArray.withIndex(): Iterable<IndexedValue<UShort>>

Returns a lazy Iterable of IndexedValue for each element of the original array.

Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)
fun <T> Iterable<T>.withIndex(): Iterable<IndexedValue<T>>

Returns a lazy Iterable of IndexedValue for each element of the original collection.

Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)
fun <T> Iterator<T>.withIndex(): Iterator<IndexedValue<T>>

Returns an Iterator wrapping each value produced by this Iterator with the IndexedValue, containing value and it's index.

import samples.Sample
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
}

© 2010–2019 JetBrains s.r.o.
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/with-index.html