Kotlin Program to Concatenate Two Arrays

Kotlin Program to Concatenate Two Arrays. In the above program, we’ve two integer arrays array1 and array2. In order to combine (concatenate) two arrays, we find its length stored in aLen and bLen respectively. Then, we create a new integer array result with length Alen + ben.

Kotlin Program to Concatenate Two Arrays

Source Code

import java.util.Arrays

fun main(args: Array<String>) {
    val array1 = intArrayOf(2, 3, 4)
    val array2 = intArrayOf(5, 6, 7)

    val aLen = array1.size
    val bLen = array2.size
    val result = IntArray(aLen + bLen)

    System.arraycopy(array1, 0, result, 0, aLen)
    System.arraycopy(array2, 0, result, aLen, bLen)

    println(Arrays.toString(result))
}

Output