Kotlin List size

To get the size of List in Kotlin, read size property of List object.

The Kotlin List.size property returns an integer representing the size of the list.

Syntax of List.size()

The syntax of List.size property is

list.size

Return Value

List.size returns the size of list as integer.

ADVERTISEMENT

Example 1: Find Size of List

In this example, we will take a list of integers and find the size of it using List.size property.

Kotlin Program

fun main(args: Array<String>) {
    val list = listOf(52, 86, 41, 85)
    val size = list.size
    print(size)
}

Output

4

There are four elements in the list. Hence the return value 4 for list.size.

Example 2: Size of List<String>

In this example, we will take a list of strings. There are three elements in the list.

Kotlin Program

fun main(args: Array<String>) {
    val list = listOf("ab", "bc", "cd")
    val size = list.size
    print(size)
}

Output

3

Conclusion

In this Kotlin Tutorial, we learned how to find the size or length of a list, using Kotlin List.size property.