This article explores different ways to copy all elements of an int array into a new array in Kotlin.

1. Using copyOf() function

The copyOf() function returns a new array, a copy of the original array, resized to the given size. To copy the complete array, pass the original array’s length to the copyOf() function.

Download Code

2. Using copyOfRange() function

The copyOfRange() function is similar to the copyOf() function, but instead of copying elements from the beginning of the array, it copies the elements within the specified range. To copy the whole array, pass the starting index as 0 and ending index equal to the array’s length to the copyOfRange() function.

Download Code

3. Using clone() function

Alternatively, you can use the extension function clone() for copying arrays.

Download Code

4. Using System.arraycopy() function

Another plausible way is to use the System.arraycopy() function for copying the array, as shown below:

Download Code

5. Using for loop

Finally, you can use a for-loop to copy elements from the source array to the destination array.

Download Code

That’s all about copying all elements of an int array into a new array in Kotlin.