In this Kotlin example, we show you how to find elements with find() and findLast() methods in Kotlin.
Kotlin - Find Array Elements Example
The example looks for the first and last even values in the array.package net.javaguides.kotlin.examples
fun main(args: Array < String > ) {
val nums = intArrayOf(1, 2, 3, 4, 5, 6, 7, 8, 9)
val firstEven = nums.find {
it % 2 == 0
}
println("The first even value is: $firstEven")
val lastEven = nums.findLast {
it % 2 == 0
}
println("The last even value is: $lastEven")
}
Output:
The first even value is: 2
The last even value is: 8
Comments
Post a Comment