Kotlin List component3()
Syntax & Examples
Syntax of List.component3()
The syntax of List.component3() extension function is:
operator fun <T> List<T>.component3(): T
This component3() extension function of List returns 3rd element from the list.
✐ Examples
1 Example
In this example,
- We create a list named
list
containing integers from 1 to 5. - We use the
component3
function to get the third element oflist
. - The third element is stored in
thirdElement
. - We print the third element using
println
.
Kotlin Program
fun main(args: Array<String>) {
val list = listOf(1, 2, 3, 4, 5)
val thirdElement = list.component3()
println("The third element is: \$thirdElement")
}
Output
The third element is: 3
2 Example
In this example,
- We create a list named
list
containing characters'a', 'b', 'c', 'd'
. - We use the
component3
function to get the third element oflist
. - The third element is stored in
thirdElement
. - We print the third element using
println
.
Kotlin Program
fun main(args: Array<String>) {
val list = listOf('a', 'b', 'c', 'd')
val thirdElement = list.component3()
println("The third element is: \$thirdElement")
}
Output
The third element is: c
3 Example
In this example,
- We create a list named
list
containing strings"apple", "banana", "cherry"
. - We use the
component3
function to get the third element oflist
. - The third element is stored in
thirdElement
. - We print the third element using
println
.
Kotlin Program
fun main(args: Array<String>) {
val list = listOf("apple", "banana", "cherry")
val thirdElement = list.component3()
println("The third element is: \$thirdElement")
}
Output
The third element is: cherry
Summary
In this Kotlin tutorial, we learned about component3() extension function of List: the syntax and few working examples with output and detailed explanation for each example.