This article explores different ways to concatenate two arrays in Kotlin. The requirement is to generate a new array that contains all elements of the first array, followed by all elements of the second array.

1. Using Plus Operator

A simple and fairly efficient solution to combine two arrays in Kotlin is with the plus operator. This is demonstrated below:

Download Code

 
An equivalent way is to use the plus() function.

Download Code

2. Using Spread Operator

This can also be done using the Spread operator (prefix the array with *). You can pass the contents of both arrays to the Array constructor using the Spread operator.

Download Code

3. Using System.arraycopy() function

The System class’s arraycopy() function provides a convenient and efficient way to copy elements of an array into another array.

Download Code

4. Using copyOf() function

You can also use the copyOf() function to copy elements of the first array, followed by the System.arraycopy() function to copy elements of the second array, as shown below:

Download Code

That’s all about concatenating two arrays in Kotlin.