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);
    println(w1)

    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")
}
Output:
banana
The first index of banana is 0
The last index of banana is 0



Comments