Kotlin List Example

In this example, we have a simple Kotlin List example. List stores elements in a specified order and provides indexed access to them.

Kotlin List Example

The example creates a list of numbers and computes some statistics.
package net.sourcecodeexamples.kotlin

fun main() {

    val nums = listOf(1, 15, 13, 8, 1, 19, 6, 12)

    val len = nums.count()
    val max = nums.max()
    val min = nums.min()
    val sum = nums.sum()
    val avg = nums.average()

    val msg = """
               max: $max, min: $min,
               count: $len, sum: $sum,
               average: $avg
              """
    println(msg.trimIndent())

}
Output:
max: 19, min: 1,
count: 8, sum: 75,
average: 9.375


Comments