Kotlin List size
Syntax & Examples
Syntax of List.size
The syntax of List.size property is:
abstract val size: IntThis 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
list1containing the integers1, 2, 3. - We then access the
sizeproperty oflist1to get the number of elements in the list. - The size of
list1is 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
list2containing the strings'a', 'b', 'c'. - We then access the
sizeproperty oflist2to get the number of elements in the list. - The size of
list2is 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
list3containing the strings'apple', 'banana', 'cherry'. - We then access the
sizeproperty oflist3to get the number of elements in the list. - The size of
list3is 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.