Q:

Scala program to merge two arrays or array buffer

belongs to collection: Scala Array Programs

0

Arrays are important data structures in programming and there may arise times when we have two different arrays and we need to merge them into one for processing. This is the case when you need array concatenation which is the process to merge to arrays by appending the elements of the second array after the elements of the first array.

Arrays in Scala are generally immutable, so we need to create a new array that will store the concatenated array in Scala. Or another method could be creating mutable arrays that will save memory. 

For merging two arrays Scala provides you multiple methods and we will discuss them one by one in this example.

All Answers

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

1) Using the concat() method

In Scala, there is a method named concat() that is used to concatenate two arrays.

Syntax:

    concat(array_1, array_2)

This method returns an array which is a concatenated array.

Program to concatenate two arrays using concat() method

object myObject {
    def main(args: Array[String]) {
        val array1 = Array(56, 12, 67) 
        print("Array 1: ")  
        for(i <- 0 to array1.length-1)
            print(array1(i)+" ")

        val array2 = Array(83, 45, 90) 
        print("\nArray 2: ")  
        for(i <- 0 to array2.length-1)
            print(array2(i)+" ")

        val array3 = Array.concat(array1, array2) 

        print("\nMerged array: ")  
        for(i <- 0 to array3.length-1)
            print(array3(i)+" ") 
    }
}

Output

Array 1: 56 12 67 
Array 2: 83 45 90 
Merged array: 56 12 67 83 45 90 

2) Using the ++ method

To merge two arrays into one ++ method can also be used.

Syntax:

    array1 ++ array2 

This will return the merge array.

Program to concatenate two arrays using ++ method

object myObject {
    def main(args: Array[String]) {
        val array1 = Array("Include", "Help") 
        print("Array 1: ")  
        for(i <- 0 to array1.length-1)
            print(array1(i)+" ")

        val array2 = Array("Learn", "Programming") 
        print("\nArray 2: ")  
        for(i <- 0 to array2.length-1)
            print(array2(i)+" ")

        val array3 = array1 ++ array2
        print("\nMerged array: ")  
        for(i <- 0 to array3.length-1)
            print(array3(i)+" ") 
    }
}

Output

Array 1: Include Help 
Array 2: Learn Programming 
Merged array: Include Help Learn Programming 

Merging two mutable arrays (arrayBuffers)

We can assign the concatenated array to any of the existing arrays when they are mutable. Both methods, concat() and ++ apply to ArrayBuffer also. A method assignment method can also be applied to the ArrayBuffer which is +=.

Program to concatenate two ArrayBuffer into one

import scala.collection.mutable.ArrayBuffer

object myObject {
    def main(args: Array[String]) {
        val array1 = ArrayBuffer(12, 43, 54) 
        print("Array 1: ")  
        for(i <- 0 to array1.length-1)
            print(array1(i)+" ")

        val array2 = ArrayBuffer(465, 787, 99) 
        print("\nArray 2: ")  
        for(i <- 0 to array2.length-1)
            print(array2(i)+" ")

        array1 ++= array2
        
        print("\nMerged array: ")  
        for(i <- 0 to array1.length-1)
            print(array1(i)+" ") 
    }
}

Output

Array 1: 12 43 54 
Array 2: 465 787 99 
Merged array: 12 43 54 465 787 99

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
How to extract unique elements from sequences in S... >>
<< Scala program to convert multiline strings to an a...