foreachindexed Kotlin Example

Tutorial “foreachindexed Kotlin Example”

In the tutorial, I will show you how to use Kotlin forEachIndexed method to loop through Kotlin Array, List, Map collections.

Kotlin foreachindexed method

forEachIndexed method performs the given action on each element, providing sequential index with the element.


inline fun <T> Array<out T>.forEachIndexed(
    action: (index: Int, T) -> Unit)
inline fun ByteArray.forEachIndexed(
    action: (index: Int, Byte) -> Unit)
inline fun ShortArray.forEachIndexed(
    action: (index: Int, Short) -> Unit)
inline fun IntArray.forEachIndexed(
    action: (index: Int, Int) -> Unit)
inline fun LongArray.forEachIndexed(
    action: (index: Int, Long) -> Unit)
inline fun FloatArray.forEachIndexed(
    action: (index: Int, Float) -> Unit)
inline fun DoubleArray.forEachIndexed(
    action: (index: Int, Double) -> Unit)
inline fun BooleanArray.forEachIndexed(
    action: (index: Int, Boolean) -> Unit)
inline fun CharArray.forEachIndexed(
    action: (index: Int, Char) -> Unit)
inline fun <T> Iterable<T>.forEachIndexed(
    action: (index: Int, T) -> Unit)
@ExperimentalUnsignedTypes inline fun UIntArray.forEachIndexed(
    action: (index: Int, UInt) -> Unit)
@ExperimentalUnsignedTypes inline fun ULongArray.forEachIndexed(
    action: (index: Int, ULong) -> Unit)
@ExperimentalUnsignedTypes inline fun UByteArray.forEachIndexed(
    action: (index: Int, UByte) -> Unit)
@ExperimentalUnsignedTypes inline fun UShortArray.forEachIndexed(
    action: (index: Int, UShort) -> Unit)

Performs the given action on each element, providing sequential index with the element.

Parameters: action – function that takes the index of an element and the element itself and performs the action on the element.

forEachIndexed Example with Array

Method Signature:

public inline fun <T> Array<out T>.forEachIndexed(action: (index: Int, T) -> Unit): Unit

Practice – foreachindexed Example with Array:


simpleArray.forEachIndexed{index, element -> println("index = $index, element = $element")}
// print on console
// ->
/*
  index = 0, element = 1
  index = 1, element = 2
  index = 2, element = 3
  index = 3, element = 4 
*/
customerArray.forEachIndexed{index, customer -> println("index = $index, customer = $customer")}
// print on console
// ->
/*
  index = 0, customer = Customer(name=Craig, age=45)
  index = 1, customer = Customer(name=Amos, age=23)
  index = 2, customer = Customer(name=Jack, age=20)
*/

foreachindexed Example with List

Method signature:

public inline fun <T> Iterable<T>.forEachIndexed(action: (index: Int, T) -> Unit): Unit

Practice – foreachindexed Kotlin Example with List:


simpleList.forEachIndexed{index, element -> println("index = $index, element = $element")}
// print on console
// ->
/*
  index = 0, element = 5
  index = 1, element = 6
  index = 2, element = 7
  index = 3, element = 8
*/
 
customerList.forEachIndexed{index, customer -> println("index = $index, customer = $customer")}
// print on console
// ->
/*
  index = 0, customer = Customer(name=Smith, age=26)
  index = 1, customer = Customer(name=Peter, age=43)
  index = 2, customer = Customer(name=Mary, age=27)
*/

Full Sourcecode

Here is the full sourcecode with the foreachindexed kotlin method. We create 2 data-structure: Kotlin Array and Kotlin List, then apply the foreachindexed method with the 2 collections in a main function.


package com.loizenjava.kotlinforeach
 
data class Customer(
  val name: String,
  val age: Int
)
 
fun main(args : Array<String>) {
   
  val simpleArray = arrayOf(1, 2, 3,  4)  
  val customerArray = arrayOf(Customer("Craig", 45),
                  Customer("Amos", 23),
                  Customer("Jack", 20))
    
  // 1. work with Array
  println("-------------1. work with Array-------------")
  simpleArray.forEachIndexed{index, element -> println("index = $index, element = $element")}
  // print on console
  // ->
  /*
    index = 0, element = 1
    index = 1, element = 2
    index = 2, element = 3
    index = 3, element = 4 
   */
  customerArray.forEachIndexed{index, customer -> println("index = $index, customer = $customer")}
  // print on console
  // ->
  /*
    index = 0, customer = Customer(name=Craig, age=45)
    index = 1, customer = Customer(name=Amos, age=23)
    index = 2, customer = Customer(name=Jack, age=20)
   */
  
  // 2. work with List
  println("-------------2. work with List-------------")
  simpleList.forEachIndexed{index, element -> println("index = $index, element = $element")}
  // print on console
  // ->
  /*
    index = 0, element = 5
    index = 1, element = 6
    index = 2, element = 7
    index = 3, element = 8
   */
  
  customerList.forEachIndexed{index, customer -> println("index = $index, customer = $customer")}
  // print on console
  // ->
  /*
    index = 0, customer = Customer(name=Smith, age=26)
    index = 1, customer = Customer(name=Peter, age=43)
    index = 2, customer = Customer(name=Mary, age=27)
   */
}

Reference: foreachindexed Kotlin Method

Related Tutorial: