Scala program to concatenate two integer arrays

Here, we are going to learn how to concatenate two integer arrays in Scala programming language?
Submitted by Nidhi, on May 07, 2021 [Last updated : March 10, 2023]

Scala – Concatenate Two Arrays

Here, we will create two arrays of integer elements. And, we will concatenate both arrays using the Array.concat() method and then print the concatenated array elements on the console screen.

Scala code to concatenate two integer arrays

The source code to concatenate two integer arrays is given below. The given program is compiled and executed on the ubuntu 18.04 operating system successfully.

// Scala program to concatenate two integer arrays

object Sample {  
    def main(args: Array[String]) {  
        var IntArr1 = Array(10, 20, 30, 40,50)
        var IntArr2 = Array(60, 70)
        
        // Concatenate two integer arrays
        var IntArr3 = Array.concat( IntArr1, IntArr2)
        
        println("Array element are: ")
        for ( item <- IntArr3 ) 
        {
            printf("%d ", item)
        }
        
        println()
    }
}

Output

Array element are: 
10 20 30 40 50 60 70 

Explanation

In the above program, we used an object-oriented approach to create the program. We created an object Sample, and we defined main() function. The main() function is the entry point for the program.

In the main() function, we created two integer array IntArr1, IntArr2. Then we concatenated both arrays using the Array.concat() method and assigned the result into the IntArr3 array. After that, we printed the elements of IntArr3 elements on the console screen.

Scala Array Programs »



Related Programs



Comments and Discussions!

Load comments ↻





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