Scala program to merge two integer arrays into a third array

Here, we are going to learn how to merge two integer arrays into a third array in Scala programming language?
Submitted by Nidhi, on May 08, 2021 [Last updated : March 10, 2023]

Scala – Merge Two Arrays

Here, we will create two integer arrays and then we will merge elements of both arrays into a third array.

Scala code to merge two integer arrays into a third array

The source code to merge two integer arrays into a third array is given below. The given program is compiled and executed on the ubuntu 18.04 operating system successfully.

// Scala program to merge two integer arrays 
// into third array

object Sample {  
    def main(args: Array[String]) {  
        var IntArray1 = Array(10,11,12,13,14,15)
        var IntArray2 = Array(20,21,22,23,24,25)
        var IntArray3 = new Array[Int](12)
        var count:Int=0
        var count1:Int=0
        
        // Merge IntArray1 and IntArray2 into IntArray3.
        while(count<12)
        {
            if(count<6)
            IntArray3(count)=IntArray1(count)
            else
            {
                IntArray3(count)=IntArray2(count1)
                count1=count1+1
            }
            count=count+1
        }
        
        println("Elements of merged array:")
        count=0
        while(count<12)
        {
            printf("%d ",IntArray3(count))
            count=count+1
        }
        
        println()
    }
} 

Output

Elements of merged array:
10 11 12 13 14 15 20 21 22 23 24 25

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 arrays IntArray1, IntArray2. Both arrays contain 6-6 elements. Then we merged elements of both arrays into a third array IntArray3. After that, we printed the elements of the merged array on the console screen.

Scala Array Programs »



Related Programs



All Scala Programs

Comments and Discussions!

Load comments ↻





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