Kotlin find index of first element in an iterable/list

Kotlin program to find out the index of the first element in an iterable or list :

Kotlin provides one method called indexOfFirst that returns the index of the first element of an iterable or list. In this tutorial, I will show you how to use this method with different examples.

Definition of indexOfFirst :

indexOfFirst is defined as below :

For iterable :

inline fun <t> Iterable<t>.indexOfFirst(
    predicate: (T) -> Boolean
): Int

For List :

inline fun <t> List<t>.indexOfFirst(
    predicate: (T) -> Boolean
): Int

Parameters and return values :

It takes one predicate as the parameter. This predicate returns one boolean value.

It returns one Int. It finds the first element that matches the predicate It returns -1 if the element is not found. Else, it returns the index of the found element.

Example of indexOfFirst :

fun main() {
    val strList = listOf("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")

    print(strList.indexOfFirst { it == "Sunday" })
}

Here, strList is a list of strings and we are finding out the index of the first element that equals to Sunday.

It will print 6 as the output.

If we have more than one “Sunday”, then it will return the index of the first. For example, for the below program :

fun main() {
    val strList = listOf("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")

    print(strList.indexOfFirst { it == "Sunday" })
}

It will print 0.

Example of indexOfFirst with an array :

It works similarly with an array. For example :

fun main() {
    val intArray = arrayOf(1,2,3,4,5,6,7,8,9)

    print(intArray.indexOfFirst { it == 8 })
}

It will print 7.

This example uses one IntArray but we can also used with ShortArray, ByteArray, LongArray, IntArray, DoubleArray, FloatArray, CharArray or BooleanArray.

indexOfFirst to find in an object array :

We can also use indexOfFirst with an array of objects. For example :

data class Student(val name: String, val age: Int)

fun main() {
    val studentList = arrayListOf(Student("Albert", 20), Student("Ross", 22), Student("Percy", 23), Student("Rosy", 22))

    print(studentList.indexOfFirst { it.age == 22 })
}

Here, we have one data class Student to hold the information of a Student i.e. name and age. We are using arrayListOf to create one array of Student objects. indexOfFirst is finding out the index of first Student object with age value equals to 22.

It will print 1 as the output.

Final program :

data class Student(val name: String, val age: Int)

fun main() {
    val strList = listOf("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")
    val intArray = arrayOf(1,2,3,4,5,6,7,8,9)
    val studentList = arrayListOf(Student("Albert", 20), Student("Ross", 22), Student("Percy", 23), Student("Rosy", 22))

    println(strList.indexOfFirst { it == "Sunday" })
    println(intArray.indexOfFirst { it == 8 })
    println(studentList.indexOfFirst { it.age == 22 })
}

It will print :

6
7
1

Kotlin indexOfFirst example

Similar tutorials :

Where is the color and why codevscolor ?

Long story short, I love paintings and I paint on weekends. We(me and my wife) have one Youtube channel. Below is a video that I did recently. If you love this please do subscribe to support us 😊❤️❤️