This article explores different ways to get a subarray of an array between the specified range in Kotlin.

1. Using copyOfRange() function

The standard way to get a subarray of an array in Kotlin is to use the extension function copyOfRange(), which returns a new array, a copy of the specified range of the original array.

Download Code

2. Using System.arraycopy() function

The System.arraycopy() function can also be used to get a copy from the specified range in the source array to the destination array.

Download Code

3. Using subList() with toTypedArray() function

Another plausible way of getting a subarray from an array in the specific range involves using a List. The idea is to convert the array into a list and then use the subList() function to get elements in the specified range. We can then call the toTypedArray() function to convert elements into a new array.

Download Code

4. Using map() with toTypedArray() function

We can also use the map() function to get a list of elements in the specified range, as shown below:

Download Code

5. Custom Routine

We can also write our own custom function to copy elements in the specified range from the source array into the new array.

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