Join two arrays in Kotlin
This article explores different ways to join two arrays in Kotlin.
The solution should generate a new array containing all elements of the first array, followed by all the second array elements while preserving the original order of elements in both arrays.
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:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
fun <T> join(first: Array<T>, second: Array<T>): Array<T> { return first + second } fun main() { val first = arrayOf("A", "B") val second = arrayOf("C", "D", "E") val result = join(first, second) println(result.contentToString()) // [A, B, C, D, E] } |
An equivalent way is to use the plus() function.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
fun <T> join(first: Array<T>, second: Array<T>): Array<T> { return first.plus(second) } fun main() { val first = arrayOf("A", "B") val second = arrayOf("C", "D", "E") val result = join(first, second) println(result.contentToString()) // [A, B, C, D, E] } |
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.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
inline fun <reified T> join(first: Array<T>, second: Array<T>): Array<T> { return arrayOf(*first, *second) } fun main() { val first = arrayOf("A", "B") val second = arrayOf("C", "D", "E") val result = join(first, second) println(result.contentToString()) // [A, B, C, D, E] } |
3. Using MutableList
You can also create a MutableList object and add all elements of the first array, followed by all elements of the second array. Finally, return the list as an array.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
inline fun <reified T> join(first: Array<T>, second: Array<T>): Array<T> { val list : MutableList<T> = ArrayList(); list.addAll(first); list.addAll(second); return list.toTypedArray() } fun main() { val first = arrayOf("A", "B") val second = arrayOf("C", "D", "E") val result = join(first, second) println(result.contentToString()) // [A, B, C, D, E] } |
This can be shortened to:
|
1 2 3 4 5 |
inline fun <reified T> join(first: Array<T>, second: Array<T>): Array<T> { val list = first.toMutableList(); list.addAll(second); return list.toTypedArray() } |
4. Using System.arraycopy() function
The idea is to allocate memory to the new array to accommodate all the elements present in both arrays and use System.arraycopy() to copy given arrays into the new array, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
inline fun <reified T> join(first: Array<T>, second: Array<T>): Array<T?> { val arr = arrayOfNulls<T>(first.size + second.size) System.arraycopy(first, 0, arr, 0, first.size) System.arraycopy(second, 0, arr, first.size, second.size) return arr } fun main() { val first = arrayOf("A", "B") val second = arrayOf("C", "D", "E") val result = join(first, second) println(result.contentToString()) // [A, B, C, D, E] } |
You can replace the first System.arraycopy() call to the copyOf() function:
|
1 2 3 4 5 |
fun <T> join(first: Array<T>, second: Array<T>): Array<T?> { val result = first.copyOf(first.size + second.size) System.arraycopy(second, 0, result, first.size, second.size) return result } |
5. Using Java 8 Stream
We can use Stream in Kotlin to merge two arrays. This can be easily done using the Stream.concat() function:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import java.util.* import java.util.stream.Stream fun <T> join(first: Array<T>, second: Array<T>): Array<Any> { return Stream.concat(Arrays.stream(first), Arrays.stream(second)) .toArray() } fun main() { val first = arrayOf("A", "B") val second = arrayOf("C", "D", "E") val result = join(first, second) println(result.contentToString()) // [A, B, C, D, E] } |
That’s all about joining two arrays in Kotlin.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)