Traverse a List with index in Kotlin
This article explores different ways to traverse a list with the index in Kotlin.
1. Using ListIterator
A simple solution is to use the nextIndex() function from the iterator returned by the ListIterator. It returns the index of an element which will be returned by a subsequent call to the next() function. It should be invoked before calling the next() function. Alternatively, we can invoke the previousIndex() function after calling the next() function.
The following code example shows invocation for this method:
|
1 2 3 4 5 6 7 8 |
fun main() { val collection = listOf(4, 2, 5, 4, 1) val it = collection.listIterator() while (it.hasNext()) { println("Index: ${it.nextIndex()}, Item: ${it.next()}") } } |
Output:
Index: 0, Item: 4
Index: 1, Item: 2
Index: 2, Item: 5
Index: 3, Item: 4
Index: 4, Item: 1
2. Using Loop
Another option is to maintain a primitive counter, starting with 0, and increment the counter by 1 with each iteration of the loop. This can be implemented as follows in Kotlin.
|
1 2 3 4 5 6 7 8 |
fun main() { val collection = listOf(4, 2, 5, 4, 1) var counter = 0 for (value in collection) { println("Index: {counter++}, Item: $value") } } |
Output:
Index: 0, Item: 4
Index: 1, Item: 2
Index: 2, Item: 5
Index: 3, Item: 4
Index: 4, Item: 1
Alternatively, we can iterate over the range of indices, and use it to get the index and the corresponding element.
|
1 2 3 4 5 6 7 |
fun main() { val collection = listOf(4, 2, 5, 4, 1) for (i in collection.indices) { println("Index: $i, Item: {collection[i]}") } } |
Output:
Index: 0, Item: 4
Index: 1, Item: 2
Index: 2, Item: 5
Index: 3, Item: 4
Index: 4, Item: 1
Here’s an equivalent version using the forEach() method:
|
1 2 3 4 5 |
fun main() { val collection = listOf(4, 2, 5, 4, 1) collection.indices.forEach { println("Index: $it, Item: {collection[it]}") } } |
Output:
Index: 0, Item: 4
Index: 1, Item: 2
Index: 2, Item: 5
Index: 3, Item: 4
Index: 4, Item: 1
3. Using forEachIndexed() function
Another viable alternative is to leverage the forEachIndexed() function, which provides a sequential index with the element and performs an action on each element. This would translate to a simple code below:
|
1 2 3 4 5 6 7 |
fun main() { val collection = listOf(4, 2, 5, 4, 1) collection.forEachIndexed { index, value -> println("Index: $index, Item: $value") } } |
Output:
Index: 0, Item: 4
Index: 1, Item: 2
Index: 2, Item: 5
Index: 3, Item: 4
Index: 4, Item: 1
4. Using withIndex() function
An alternative idea is to use the withIndex() function that wraps each element of a list into an IndexedValue containing the index of that element and the element itself. This is demonstrated below:
|
1 2 3 4 5 6 7 |
fun main() { val collection = listOf(4, 2, 5, 4, 1) for ((index, value) in collection.withIndex()) { println("Index: $index, Item: $value") } } |
Output:
Index: 0, Item: 4
Index: 1, Item: 2
Index: 2, Item: 5
Index: 3, Item: 4
Index: 4, Item: 1
That’s all about traversing a list with the index in Kotlin.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)