Kotlin program to concatenate two integer arrays

Kotlin | Concatenating two arrays: Here, we are going to learn how to concatenate given two integer arrays in Kotlin programming language?
Submitted by IncludeHelp, on May 03, 2020

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]

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


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.