https://kotlinlang.org logo
Title
i

Ikem Nwodo

10/07/2018, 10:04 PM
Please I need this to be explained : val collections = listOf(1, 2, 3, 5) for ((index, element) in collections.withIndex())
d

Dico

10/07/2018, 10:09 PM
First,
.withIndex()
is an extension function that associates all elements to their index using a
Pair
. So you get an
Iterable<Pair<Int, T>
.
Second,
Pair
is a data class. Which provides
component1()
and
component2()
functions for its properties in the order they appear in the constructor.
(index, element)
is deconstruction syntax, that uses those component functions in the same order.