To create a list of lists in Kotlin, you can use the `listOf` function to initialize the inner lists and then add them to the outer list.
In this example,
list1
and list2
with some elements.listOfLists
.list1
and list2
to the outer list listOfLists
.fun main() {
val list1 = listOf(1, 2, 3)
val list2 = listOf("a", "b", "c")
val listOfLists = listOf(list1, list2)
println("List of lists: $listOfLists")
}
List of lists: [[1, 2, 3], [a, b, c]]
In this example,
list3
and list4
.listOfListsEmpty
.list3
and list4
to the outer list listOfListsEmpty
.fun main() {
val list3 = listOf<Int>()
val list4 = listOf<String>()
val listOfListsEmpty = listOf(list3, list4)
println("List of lists with empty inner lists: $listOfListsEmpty")
}
List of lists with empty inner lists: [[], []]
In this tutorial, we learned How to create a List of Lists in Kotlin language with well detailed examples.