Kotlin .reverse()

Sriparno08's avatar
Published Dec 6, 2024
Contribute to Docs

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

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn Kotlin, the expressive, open-source programming language developed by JetBrains.
    • Beginner Friendly.
      9 hours

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

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn Kotlin, the expressive, open-source programming language developed by JetBrains.
    • Beginner Friendly.
      9 hours