To get the last element in a Kotlin list, you can use the `last()` method.
In this example,
numbers
with integer elements.last()
method.fun main() {
val numbers = mutableListOf(10, 20, 30, 40, 50)
val lastElement = numbers.last()
println("Last element is: $lastElement")
}
Last element is: 50
In this example,
names
with string elements.last()
method.fun main() {
val names = mutableListOf("Alice", "Bob", "Charlie", "David")
val lastElement = names.last()
println("Last element is: $lastElement")
}
Last element is: David
In this tutorial, we learned How to get Last Element in a List in Kotlin language with well detailed examples.