Kotlin array count example

In this Kotlin example, we will see how to count the total number of values and the number of even values in the array
All Kotlin examples at All Kotlin Examples
The count() method counts the elements in the array.

Kotlin array count example

The example counts the total number of values and the number of even values in the array.
fun main() {

    val nums = intArrayOf(2, 3, 4, 5, 6, 7)

    println("There are ${nums.count()} elements in the array")

    val nOfEvens = nums.count { it % 2 == 0 }

    println("There are $nOfEvens even values in the array")
}
Output:
There are 6 elements in the array
There are 3 even values in the array
All Kotlin examples at All Kotlin Examples.


Comments