last

Common
JVM
JS
Native
1.0
fun < T > Array < out T > . last ( ) : T
(source)
fun ByteArray . last ( ) : Byte
(source)
fun ShortArray . last ( ) : Short
(source)
fun IntArray . last ( ) : Int
(source)
fun LongArray . last ( ) : Long
(source)
fun FloatArray . last ( ) : Float
(source)
fun DoubleArray . last ( ) : Double
(source)
fun BooleanArray . last ( ) : Boolean
(source)
fun CharArray . last ( ) : Char
(source)
@ExperimentalUnsignedTypes fun UIntArray . last ( ) : UInt
(source)
@ExperimentalUnsignedTypes fun ULongArray . last ( ) : ULong
(source)
@ExperimentalUnsignedTypes fun UByteArray . last ( ) : UByte
(source)
@ExperimentalUnsignedTypes fun UShortArray . last ( ) : UShort
(source)

Returns the last element.

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val list = listOf(1, 2, 3, 4)
println(list.last()) // 4
println(list.last { it % 2 == 1 }) // 3
println(list.lastOrNull { it < 0 }) // null
// list.last { it < 0 } //  will fail

val emptyList = emptyList<Int>()
println(emptyList.lastOrNull()) // null
// emptyList.last() //  will fail
//sampleEnd
}

Exceptions

NoSuchElementException - if the array is empty.

Common
JVM
JS
Native
1.0
inline fun < T > Array < out T > . last (
predicate : ( T ) -> Boolean
) : T

(source)
inline fun ByteArray . last ( predicate : ( Byte ) -> Boolean ) : Byte
(source)
inline fun ShortArray . last (
predicate : ( Short ) -> Boolean
) : Short

(source)
inline fun IntArray . last ( predicate : ( Int ) -> Boolean ) : Int
(source)
inline fun LongArray . last ( predicate : ( Long ) -> Boolean ) : Long
(source)
inline fun FloatArray . last (
predicate : ( Float ) -> Boolean
) : Float

(source)
inline fun DoubleArray . last (
predicate : ( Double ) -> Boolean
) : Double

(source)
inline fun BooleanArray . last (
predicate : ( Boolean ) -> Boolean
) : Boolean

(source)
inline fun CharArray . last ( predicate : ( Char ) -> Boolean ) : Char
(source)
inline fun < T > Iterable < T > . last ( predicate : ( T ) -> Boolean ) : T
(source)
inline fun < T > List < T > . last ( predicate : ( T ) -> Boolean ) : T
(source)
@ExperimentalUnsignedTypes inline fun UIntArray . last (
predicate : ( UInt ) -> Boolean
) : UInt

(source)
@ExperimentalUnsignedTypes inline fun ULongArray . last (
predicate : ( ULong ) -> Boolean
) : ULong

(source)
@ExperimentalUnsignedTypes inline fun UByteArray . last (
predicate : ( UByte ) -> Boolean
) : UByte

(source)
@ExperimentalUnsignedTypes inline fun UShortArray . last (
predicate : ( UShort ) -> Boolean
) : UShort

(source)

Returns the last element matching the given predicate .

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val list = listOf(1, 2, 3, 4)
println(list.last()) // 4
println(list.last { it % 2 == 1 }) // 3
println(list.lastOrNull { it < 0 }) // null
// list.last { it < 0 } //  will fail

val emptyList = emptyList<Int>()
println(emptyList.lastOrNull()) // null
// emptyList.last() //  will fail
//sampleEnd
}

Exceptions

NoSuchElementException - if no such element is found.

Common
JVM
JS
Native
1.0
fun < T > Iterable < T > . last ( ) : T
(source)

Returns the last element.

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val list = listOf(1, 2, 3, 4)
println(list.last()) // 4
println(list.last { it % 2 == 1 }) // 3
println(list.lastOrNull { it < 0 }) // null
// list.last { it < 0 } //  will fail

val emptyList = emptyList<Int>()
println(emptyList.lastOrNull()) // null
// emptyList.last() //  will fail
//sampleEnd
}

Exceptions

NoSuchElementException - if the collection is empty.

Common
JVM
JS
Native
1.0
fun < T > List < T > . last ( ) : T
(source)

Returns the last element.

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val list = listOf(1, 2, 3, 4)
println(list.last()) // 4
println(list.last { it % 2 == 1 }) // 3
println(list.lastOrNull { it < 0 }) // null
// list.last { it < 0 } //  will fail

val emptyList = emptyList<Int>()
println(emptyList.lastOrNull()) // null
// emptyList.last() //  will fail
//sampleEnd
}

Exceptions

NoSuchElementException - if the list is empty.