Q:

Scala program to concatenate two integer arrays

belongs to collection: Scala Array Programs

0

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.

All Answers

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

Program/Source Code:

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 IntArr1IntArr2. 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.

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

total answers (1)

Scala Array Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Scala program to merge two integer arrays into a t... >>
<< Scala program to insert an item into an array...