Kotlin List component4()
Syntax & Examples
Syntax of List.component4()
The syntax of List.component4() extension function is:
operator fun <T> List<T>.component4(): T
This component4() extension function of List returns 4th element from the list.
✐ Examples
1 Example
In this example,
- We create a list named
list1
containing the integers10, 20, 30, 40, 50
. - We use the
component4()
extension function to get the 4th element from the list. - The value of the 4th element is stored in
element
. - Finally, we print the value of
element
to standard output using println.
Kotlin Program
fun main(args: Array<String>) {
val list1 = listOf(10, 20, 30, 40, 50);
val element = list1.component4();
println(element);
}
Output
40
2 Example
In this example,
- We create a list named
list1
containing the strings"apple", "banana", "cherry", "date", "elderberry"
. - We use the
component4()
extension function to get the 4th element from the list. - The value of the 4th element is stored in
fruit
. - Finally, we print the value of
fruit
to standard output using println.
Kotlin Program
fun main(args: Array<String>) {
val list1 = listOf("apple", "banana", "cherry", "date", "elderberry");
val fruit = list1.component4();
println(fruit);
}
Output
date
Summary
In this Kotlin tutorial, we learned about component4() extension function of List: the syntax and few working examples with output and detailed explanation for each example.