To get the length of a list in Kotlin, you can use the size
property of the List class.
In this example,
numbers
with integer values.size
property of the list object numbers
to get the number of elements in the list.size
property returns the number of elements in the list.fun main() {
val numbers = listOf(1, 2, 3, 4, 5)
val length = numbers.size
println("Length of the list is: $length")
}
Length of the list is: 5
In this example,
fruits
with string values.size
property of the list object fruits
to get the number of elements in the list.size
property returns the number of elements in the list.fun main() {
val fruits = listOf("apple", "banana", "cherry")
val length = fruits.size
println("Length of the list is: $length")
}
Length of the list is: 3
In this tutorial, we learned How to Get Length of a List in Kotlin language with well detailed examples.