To get the length of an array in Kotlin, you can use the size
property of the array.
In this example,
numbers
with some values.size
property of the array object numbers
, and store the returned value in length
variable.Array.size
property returns the length of this array.length
to standard output.fun main() {
val numbers = intArrayOf(1, 2, 3, 4, 5);
val length = numbers.size;
println("Length of array is: $length");
}
Length of array is: 5
In this example,
colors
with some values.size
property of the array object colors
, and store the returned value in length
variable.Array.size
property returns the length of this array.length
to standard output.fun main() {
val colors = arrayOf("red", "green", "blue");
val length = colors.size;
println("Length of array is: $length");
}
Length of array is: 3
In this tutorial, we learned How to Get Array Length in Kotlin language with well detailed examples.