Home » Scala » Scala array programs

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

Scala | Merging two arrays

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.

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





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.