.reverse()

Anonymous contributor's avatar
Anonymous contributor
Published Dec 6, 2024
Contribute to Docs

In Kotlin, the .reverse() function reverses the order of the elements in-place in a specified array.

Syntax

arr.reverse()
  • arr: A mutable array whose elements will be reversed in place.

Example

The following example demonstrates the usage of the .reverse() function:

fun main() {
// Creating an array
val arr = intArrayOf(3, 7, 11, 15, 19)
// Reversing the order of the elements in the array
arr.reverse()
// Printing the modified array
println(arr.contentToString())
}

The above code produces the following output:

[19, 15, 11, 7, 3]

All contributors

Contribute to Docs

Learn Kotlin on Codecademy