Skip to main content
ubuntuask.com

Back to all posts

How to Reverse an Arraylist In Kotlin?

Published on
2 min read
How to Reverse an Arraylist In Kotlin? image

Best Kotlin Programming Books to Buy in July 2026

1 Kotlin for Beginners: The Complete Hands-On Guide to Learn Kotlin Programming and Build Real Android Apps – Even If You’ve Never Coded Before

Kotlin for Beginners: The Complete Hands-On Guide to Learn Kotlin Programming and Build Real Android Apps – Even If You’ve Never Coded Before

BUY & SAVE
$9.99
Kotlin for Beginners: The Complete Hands-On Guide to Learn Kotlin Programming and Build Real Android Apps – Even If You’ve Never Coded Before
2 Kotlin in Action, Second Edition

Kotlin in Action, Second Edition

BUY & SAVE
$46.89 $59.99
Save 22%
Kotlin in Action, Second Edition
3 Competitive Programming 4 - Book 1: The Lower Bound of Programming Contests in the 2020s

Competitive Programming 4 - Book 1: The Lower Bound of Programming Contests in the 2020s

BUY & SAVE
$20.79
Competitive Programming 4 - Book 1: The Lower Bound of Programming Contests in the 2020s
4 Kotlin from Scratch: A Project-Based Introduction for the Intrepid Programmer

Kotlin from Scratch: A Project-Based Introduction for the Intrepid Programmer

BUY & SAVE
$35.10 $59.99
Save 41%
Kotlin from Scratch: A Project-Based Introduction for the Intrepid Programmer
5 How to Build Android Applications with Kotlin: A hands-on guide to developing, testing, and publishing production-grade Android 16 apps

How to Build Android Applications with Kotlin: A hands-on guide to developing, testing, and publishing production-grade Android 16 apps

BUY & SAVE
$42.49 $49.99
Save 15%
How to Build Android Applications with Kotlin: A hands-on guide to developing, testing, and publishing production-grade Android 16 apps
6 Functional Programming in Kotlin

Functional Programming in Kotlin

BUY & SAVE
$44.01 $49.99
Save 12%
Functional Programming in Kotlin
7 Programming Android with Kotlin: Achieving Structured Concurrency with Coroutines

Programming Android with Kotlin: Achieving Structured Concurrency with Coroutines

BUY & SAVE
$48.00 $65.99
Save 27%
Programming Android with Kotlin: Achieving Structured Concurrency with Coroutines
8 Kotlin Programming, In 8 Hours, For Beginners, Learn Coding Fast: Kotlin Language Crash Course Textbook & Exercises (Cookbooks in 8 Hours)

Kotlin Programming, In 8 Hours, For Beginners, Learn Coding Fast: Kotlin Language Crash Course Textbook & Exercises (Cookbooks in 8 Hours)

BUY & SAVE
$2.99
Kotlin Programming, In 8 Hours, For Beginners, Learn Coding Fast: Kotlin Language Crash Course Textbook & Exercises (Cookbooks in 8 Hours)
+
ONE MORE?

To reverse an ArrayList in Kotlin, you can simply use the reverse() function provided by the Kotlin standard library. This function will reverse the order of elements in the ArrayList.

How to reverse an ArrayList using Kotlin extensions?

You can reverse an ArrayList using a Kotlin extension function like this:

fun ArrayList.reverse() { val size = this.size for (i in 0 until size / 2) { val temp = this[i] this[i] = this[size - i - 1] this[size - i - 1] = temp } }

You can then use this extension function on any ArrayList like this:

val list = arrayListOf(1, 2, 3, 4, 5) list.reverse() println(list) // Output: [5, 4, 3, 2, 1]

How to reverse an ArrayList while preserving the original order in Kotlin?

To reverse an ArrayList while preserving the original order in Kotlin, you can create a copy of the original ArrayList and then reverse the copy using the built-in asReversed() function. Here is an example of how to do this:

fun main() { val originalList = arrayListOf(1, 2, 3, 4, 5)

val reversedList = originalList.toList().asReversed()

println("Original list: $originalList")
println("Reversed list: $reversedList")

}

This will output:

Original list: [1, 2, 3, 4, 5] Reversed list: [5, 4, 3, 2, 1]

In this example, toList() is used to create a new List from the original ArrayList to prevent modifications to the original list when reversing it. Then, asReversed() is called on the new List to reverse its elements while preserving the order of the original list.

What is the performance impact of reversing an ArrayList in Kotlin?

Reversing an ArrayList in Kotlin has a time complexity of O(n), where n is the size of the ArrayList. This means that the performance impact of reversing an ArrayList increases linearly with the size of the ArrayList.

In practice, for small or medium-sized ArrayLists, the performance impact of reversing an ArrayList in Kotlin is negligible. However, for very large ArrayLists, the time taken to reverse the ArrayList may become noticeable.

It is important to consider the size of the ArrayList and the frequency of reversing operations when assessing the performance impact. In many cases, the performance impact of reversing an ArrayList in Kotlin will not be significant enough to cause any noticeable slowdown in the overall application.