Kotlin List size
Syntax & Examples
Syntax of List.size
The syntax of List.size property is:
abstract val size: Int
This size property of List returns the size of the collection.
✐ Examples
1 Get size of a list of numbers
In this example,
- We create a list named
list1
containing the integers1, 2, 3
. - We then access the
size
property oflist1
to get the number of elements in the list. - The size of
list1
is printed to standard output.
Kotlin Program
fun main(args: Array<String>) {
val list1 = listOf(1, 2, 3);
val size = list1.size
print(size);
}
Output
3
2 Get size of a list of characters
In this example,
- We create a list named
list2
containing the strings'a', 'b', 'c'
. - We then access the
size
property oflist2
to get the number of elements in the list. - The size of
list2
is printed to standard output.
Kotlin Program
fun main(args: Array<String>) {
val list2 = listOf("a", "b", "c");
val size = list2.size
print(size);
}
Output
3
3 Get size of a list of strings
In this example,
- We create a list named
list3
containing the strings'apple', 'banana', 'cherry'
. - We then access the
size
property oflist3
to get the number of elements in the list. - The size of
list3
is printed to standard output.
Kotlin Program
fun main(args: Array<String>) {
val list3 = listOf("apple", "banana", "cherry");
val size = list3.size
print(size);
}
Output
3
Summary
In this Kotlin tutorial, we learned about size property of List: the syntax and few working examples with output and detailed explanation for each example.