In this tutorial, you shall learn how to get the size of a given array in Kotlin using size property of Array class or count() method of Array class, with examples.

Kotlin Array Size

To get size of Array in Kotlin, read the size property of this Array object. size property returns number of elements in this Array.

We can also use count() function of the Array class to get the size. We will go through an example to get the size of the Array using count() function.

Syntax

The syntax to get size of Array arr, in Kotlin is

</>
Copy
arr.size

Examples

1. Get the size of given integer array

In the following example, we create a Kotlin Array with some elements, and find its size using the Array.size property.

Main.kt

</>
Copy
fun main(args: Array<String>) {
    val arr = arrayOf(2, 4, 6, 8, 10)
    val length = arr.size
    println("Array size : $length")
}

Output

Array size : 5

2. Get the size of given string array

Now, let us take an Array of strings, with three elements in it. We find out this Array size programmatically using Array.size property.

Main.kt

</>
Copy
fun main(args: Array<String>) {
    val arr = arrayOf("apple", "banana", "cherry")
    val length = arr.size
    println("Array size : $length")
}

Output

Array size : 3

3. Get the size of given array using Array.count() method

We now use count() function on Array object, and find its size.

Main.kt

</>
Copy
fun main(args: Array<String>) {
    val arr = arrayOf("a", "b", "c")
    val length = arr.count()
    println("Array size : $length")
}

Output

Array size : 3

Conclusion

In this Kotlin Tutorial, we learned how to get the size of an Array using Array.size property, with the help of examples.