This article explores different ways to get a subarray of an array between given indexes in Kotlin.

1. Using copyOfRange() function

In Kotlin, you can use the copyOfRange() to copy the given range into a new array.

Download Code

2. Using map() function

Here, the idea is to get the valid indices of the subarray in the array and transform the indices into the corresponding element in the original array and return the collection as an array.

Download Code

3. Custom Routine

Finally, you can write your custom routine, which creates a new array and copy elements from the original array to the new array by iterating over between given indexes.

Download Code

That’s all about getting a subarray of an array between given indexes in Kotlin.