To create an empty list in Kotlin, you can use the emptyList
function or the mutableListOf
function with no arguments.
In this example,
emptyList
function to create an empty immutable list of type String
.emptyList
function returns an empty list.fun main() {
val emptyList: List<String> = emptyList()
println("Empty list: $emptyList")
}
Empty list: []
In this example,
mutableListOf
function with no arguments to create an empty mutable list of type Int
.mutableListOf
function returns an empty mutable list.fun main() {
val emptyMutableList: MutableList<Int> = mutableListOf()
println("Empty mutable list: $emptyMutableList")
}
Empty mutable list: []
In this tutorial, we learned How to create an Empty List in Kotlin language with well detailed examples.