 ## 1. Overview

Arrays are a key data structure in computer science. Many other structures and collections are built on top of them.

In this quick tutorial, we’re going to see how to combine arrays in Kotlin.

## 2. What Is Combining?

First, we have to define the concept of combining.

Let’s say we have two arrays. Combining these two arrays means placing the elements of both into just one array. Other terms often used are merging or joining. In the example below, the elements of arr2 are placed at the end of arr1:

``````arr1 = [1, 2, 3]
arr2 = [4, 5, 6]``````

If we combine these two arrays, a possible result is:

``[1, 2, 3, 4, 5, 6]``

## 3. The plus Method

A Kotlin array contains the plus method. This method expects a single element, a collection, or another array. In the following sample, the elements of arr2 will be appended at the end arr1:

``````val arr1 = arrayOf(1, 2, 3)
val arr2 = arrayOf(4, 5, 6)

// [1, 2, 3, 4, 5, 6]
val mergedArray = arr1.plus(arr2)``````

## 4. The + Operator

The built-in operator, +, uses the plus method under the hood. This operator acts as a shortcut, so it is equivalent to execute the plus method. In the following example, the elements of arr2 (right operand) will be appended at the end arr1 (left operand):

``````val arr1 = arrayOf(1, 2, 3)
val arr2 = arrayOf(4, 5, 6)

// [1, 2, 3, 4, 5, 6]
val mergedArray = arr1 + arr2``````

## 5. arrayOf and the Spread Operator

The arrayOf method is the standard way to create a new array in Kotlin. It expects a varargs type (a variable number of elements) as a parameter. The spread operator transforms an array into a varargs type.

This way, we can simply pass the arrays in whichever order we prefer. Note that in this case, as we are using a generic method, we need both reified and inline keywords.

The following sample creates a new array with the elements of arr1 followed by the elements of arr2:

``````val arr1 = arrayOf(1, 2, 3)
val arr2 = arrayOf(4, 5, 6)

// [1, 2, 3, 4, 5, 6]
val mergedArray = arrayOf(*arr1, *arr2)``````

We can even include new elements in-between the arrays:

``````val arr1 = arrayOf(1, 2, 3)
val arr2 = arrayOf(4, 5, 6)
val middleElement = 0

// [4, 5, 6, 0, 1, 2, 3]
val mergedArray = arrayOf(*arr2, middleElement, *arr1)``````

## 6. Looping Over the Arrays

If we don’t want to use any built-in method, we can always create our own algorithm. In this case, it’s pretty simple.

First, we initialize a new array. Its size must be the sum of the sizes of the arrays we want to merge:

``size = (size of <em>arr1</em>) + (size of <em>arr2</em>)``

Then, we just have to iterate in order over both arrays. For each element, we insert it into the new array in order:

``````fun combine(arr1: Array<Int>, arr2: Array<Int>): Array<Int> {
val mergedArray = Array(arr1.size + arr2.size) { 0 }

var position = 0
for (element in arr1) {
mergedArray[position] = element
position++
}

for (element in arr2) {
mergedArray[position] = element
position++
}

return mergedArray
}

val arr1 = arrayOf(1, 2, 3)
val arr2 = arrayOf(4, 5, 6)

// [1, 2, 3, 4, 5, 6]
val mergedArray = combine(arr1, arr2)
``````

## 7. Conclusion

In this article, we have seen the most common ways to merge arrays in Kotlin. Using Kotlin’s built-in operators allows our code to be cleaner and follow the conventions of the language.

As usual, all the examples are available over on GitHub.