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
</>
Copy
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.
Example
In the following program, we will create a list using listOf() function, and pass lists as elements to this function.
Main.kt
</>
Copy
fun main(args: Array<String>) {
val listOfLists = listOf(
listOf(1, 3, 9),
listOf("Abc", "Xyz", "Pqr")
)
print(listOfLists)
}
Output
[[1, 3, 9], [Abc, Xyz, Pqr]]
Conclusion
In this Kotlin Tutorial, we learned how to define a List of Lists in Kotlin, using listOf() function.