Scala program to merge two arrays or array buffer

Scala | Merging two arrays: Here, we are going to learn different methods to merge two arrays in the Scala programming language.
Submitted by Shivang Yadav, on April 12, 2020 [Last updated : March 10, 2023]

Scala – Merging Two Arrays or ArrayBuffers

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

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.

Scala code to merge 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.

Scala code to merge 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 +=.

Scala code to merge 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

Scala Array Programs »



Related Programs



Comments and Discussions!

Load comments ↻





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