Using the 'withIndex' Function for Indexed Iteration

The 'withIndex' function allows you to iterate over a collection while keeping track of the index of each element. This is particularly useful when you need both the element and its position within the collection. It returns a sequence of indexed values that contain both the index and the element, making it easy to use in loops.

val items = listOf("Apple", "Banana", "Cherry")

for ((index, value) in items.withIndex()) {
    println("Item at index $index is $value")
}