Kotlin – List of Integers

To define a list of integers in Kotlin, call listOf() function and pass all the integers as arguments to it.

The syntax of listOf() function is

fun <T> listOf(vararg elements: T): List<T>

listOf() function returns a read-only List.

Since, we are passing integers for elements parameter, listOf() function returns a List<Int> object.

Example

In the following program, we will define a list of integers using listOf() function.

Main.kt

fun main(args: Array<String>) {
    val numbers = listOf(1, 3, 9, 16, 25, 36)
    print(numbers)
}

Output

[1, 3, 9, 16, 25, 36]
ADVERTISEMENT

Conclusion

In this Kotlin Tutorial, we learned how to define a list of integers in Kotlin, using listOf() function.