Kotlin Array indexOf Function

The indexOf function in Kotlin is used to find the index of the first occurrence of a specified element in an array. This function is part of the Kotlin standard library and provides a straightforward way to search for elements in an array.

Table of Contents

  1. Introduction
  2. indexOf Function Syntax
  3. Understanding indexOf
  4. Examples
    • Basic Usage
    • Using indexOf with Custom Types
    • Handling Non-Existing Elements
  5. Real-World Use Case
  6. Conclusion

Introduction

The indexOf function returns the index of the first occurrence of the specified element in the array, or -1 if the element is not found. It is a simple and effective way to search for elements in an array.

indexOf Function Syntax

The syntax for the indexOf function is as follows:

fun <T> Array<out T>.indexOf(element: T): Int

Parameters:

  • element: The element to search for in the array.

Returns:

  • The index of the first occurrence of the specified element, or -1 if the element is not found.

Understanding indexOf

The indexOf function searches the array from the beginning to the end and returns the index of the first element that matches the specified element. If the element is not found, it returns -1.

Examples

Basic Usage

To demonstrate the basic usage of indexOf, we will create an array of integers and find the index of a specified element.

Example

fun main() {
    val numbers = arrayOf(1, 2, 3, 4, 5)
    val index = numbers.indexOf(3)
    println("Index of 3: $index")
}

Output:

Index of 3: 2

Using indexOf with Custom Types

This example shows how to use indexOf to find the index of a specified element in an array of custom objects. The custom objects must implement the equals method to be comparable.

Example

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

fun main() {
    val people = arrayOf(
        Person("Ravi", 25),
        Person("Anjali", 30),
        Person("Priya", 22)
    )

    val index = people.indexOf(Person("Anjali", 30))
    println("Index of Anjali: $index")
}

Output:

Index of Anjali: 1

Handling Non-Existing Elements

This example demonstrates how to handle cases where the specified element does not exist in the array.

Example

fun main() {
    val numbers = arrayOf(1, 2, 3, 4, 5)
    val index = numbers.indexOf(6)
    if (index != -1) {
        println("Index of 6: $index")
    } else {
        println("6 is not found in the array")
    }
}

Output:

6 is not found in the array

Real-World Use Case

Searching for an Element in a List

In real-world applications, the indexOf function can be used to search for elements in a list, such as finding the position of a specific item in a list of products.

Example

data class Product(val name: String, val price: Double)

fun main() {
    val products = arrayOf(
        Product("Laptop", 999.99),
        Product("Smartphone", 699.99),
        Product("Tablet", 299.99)
    )

    val index = products.indexOf(Product("Smartphone", 699.99))
    println("Index of Smartphone: $index")
}

Output:

Index of Smartphone: 1

Conclusion

The indexOf function in Kotlin is a convenient method for finding the index of the first occurrence of a specified element in an array. It provides a simple way to search for elements and handle cases where the element is not found. By understanding and using this function, you can effectively manage search operations in your Kotlin applications.

Comments