Kotlin Set indexing Example

Each element of a set has an index. Kotlin set indexes to start from zero. The last element has len-1 index.

Kotlin Set indexing Example

The following example presents the Kotlin Set indexing operations.
package net.sourcecodeexamples.kotlin

fun main() {

    val fruits = setOf("banana", "mango", "apple", "orange", "banana")

    val w1 = fruits.elementAt(0);

    val i1 = fruits.indexOf("banana")
    println("The first index of banana is $i1")

    val i2 = fruits.lastIndexOf("banana")
    println("The last index of banana is $i2")
The first index of banana is 0
The last index of banana is 0

Free Spring Boot Tutorial - 5 Hours Full Course

Watch this course on YouTube at Spring Boot Tutorial | Fee 5 Hours Full Course