This article explores different ways to find the size of an array or a list in Kotlin.

1. Using size property

The standard solution to find the size of an array or a list in Kotlin is using its size property. This is demonstrated below for an Int Array and a List:

Download Code

2. Using count() function

Another option to count the number of elements in the array or list is using the count() function with predicate as true. This would translate to a simple code below:

Download Code

3. Using Reflection

Although not recommended, we can dynamically access properties of an array using the java.lang.reflect.Array class. To get the length of an array, use the getLength() function. Note that this only works for an array, otherwise it will result in java.lang.IllegalArgumentException: Argument is not an array exception.

Download Code

That’s all about finding the size of an array in Kotlin.