Kotlin List component5()
Syntax & Examples


Syntax of List.component5()

The syntax of List.component5() extension function is:

operator fun <T> List<T>.component5(): T

This component5() extension function of List returns 5th element from the list.



✐ Examples

1 Example

In this example,

  • We create a list named list1 containing the integers 10, 20, 30, 40, 50.
  • We use the component5() extension function to get the 5th element from the list.
  • The value of the 5th 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.component5();
    println(element);
}

Output

50

2 Example

In this example,

  • We create a list named list1 containing the strings "apple", "banana", "cherry", "date", "elderberry".
  • We use the component5() extension function to get the 5th element from the list.
  • The value of the 5th 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.component5();
    println(fruit);
}

Output

elderberry

Summary

In this Kotlin tutorial, we learned about component5() extension function of List: the syntax and few working examples with output and detailed explanation for each example.