Q:

Kotlin program to concatenate two integer arrays

belongs to collection: Kotlin Array Programs

0

Given two integer arrays, we have to concatenate them.

Example:

    Input:
    arr1 = [10, 20, 30]
    arr2 = [40, 50]

    Output:
    Concatenated array: [10, 20, 30, 40, 50]

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program to concatenate two integer arrays in Kotlin

package com.includehelp.basic

import java.util.*

//Main Function entry Point of Program
fun main(args: Array<String>) {
    //Input Stream
    val s = Scanner(System.`in`)

    //Input Array Size
    print("Enter number of elements in the array1: ")
    var size = s.nextInt()

    //Create Integer array of Given size
    val intArray1 = IntArray(size)

    //Input array elements
    println("Enter Arrays Elements:")
    for (i in intArray1.indices) {
        print("intArray1[$i] : ")
        intArray1[i] = s.nextInt()
    }

    //Input Array Size
    print("Enter number of elements in the array2: ")
    size = s.nextInt()

    //Create Integer array of Given size
    val intArray2 = IntArray(size)

    //Input array elements
    println("Enter Arrays Elements:")
    for (i in intArray2.indices) {
        print("intArray2[$i] : ")
        intArray2[i] = s.nextInt()
    }

    //Print Array1 Elements
    println("Array1 : ${intArray1.contentToString()}")

    //Print Array2 Elements
    println("Array2 : ${intArray2.contentToString()}")

    var concatArray = intArray1+intArray2

    //Print concatArray Elements and its Size
    println("concatArray Elements : ${concatArray.contentToString()}")
    println("concatArray Size     : ${concatArray.size}")
}

Output

Run 1:
-----
Enter number of elements in the array1: 5
Enter Arrays Elements:
intArray1[0] : 3
intArray1[1] : 4
intArray1[2] : 5
intArray1[3] : 6
intArray1[4] : 798
Enter number of elements in the array2: 3
Enter Arrays Elements:
intArray2[0] : 2
intArray2[1] : 1
intArray2[2] : 9
Array1 : [3, 4, 5, 6, 798]
Array2 : [2, 1, 9]
concatArray Elements : [3, 4, 5, 6, 798, 2, 1, 9]
concatArray Size     : 8
--------
Run 2:
----
Enter number of elements in the array1: 2
Enter Arrays Elements:
intArray1[0] : 6
intArray1[1] : 5
Enter number of elements in the array2: 3
Enter Arrays Elements:
intArray2[0] : 5
intArray2[1] : 3
intArray2[2] : 2
Array1 : [6, 5]
Array2 : [5, 3, 2]
concatArray Elements : [6, 5, 5, 3, 2]
concatArray Size     : 5

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Kotlin program to sort an array in ascending order... >>
<< Kotlin program to find sum and average of array el...