Kotlin – List of Lists

To define a list of Lists in Kotlin, call listOf() function and provide other Lists as elements to this function.

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 lists for elements parameter, listOf() function returns a List<List> object.


In the following program, we will create a list using listOf() function, and pass lists as elements to this function.


fun main(args: Array<String>) {
    val listOfLists = listOf(
        listOf(1, 3, 9),
        listOf("Abc", "Xyz", "Pqr")


[[1, 3, 9], [Abc, Xyz, Pqr]]


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