Kotlin – Reverse an Array

Reverse an Array in Kotlin

To reverse an Array in Kotlin, you can use Array.reverse() function. Call the reverse() function on the given array, and the function reverses the Array in-place.

The syntax to reverse the Array arr is

arr.reverse()

Examples

Reverse the Array fruitNames

In the following program, we take an Arrays of strings: fruitNames. Reverse this Array using Array.reverse() function.

Kotlin Program

fun main(args: Array<String>) {
    var fruitNames = arrayOf("apple", "banana", "cherry")
    fruitNames.reverse()
    println(fruitNames.contentToString())
}

Output

[cherry, banana, apple]