We can use the concat()
method of the array
class to concatenate or merge two arrays . This method takes two arrays and combines them to form a single array.
Array.concat(array1, array2)
array1: This is one of the two arrays that we want to merge.
array2: This is the second of the two arrays that we want to merge.
The concat()
method returns a new array that concatenates the arrays array1
and array2
.
object Main extends App {// create some arraysval randomNumbers1 = Array(34, 2, 5, 70, 1)val randomNumbers2 = Array(9, 40, 3, 0)val fruits1 = Array("Pineapple", "Banana", "Orange")val fruits2 = Array("Apple", "Water-melon")val cgpas1 : Array[Double] = Array(3.4, 4.5, 2.5, 0.5)val cgpas2 : Array[Double] = Array(5.0, 1.5)// concat arraysval randomNumbers = Array.concat(randomNumbers1, randomNumbers2)val fruits = Array.concat(fruits1, fruits2)val cgpas = Array.concat(cgpas1, cgpas2)// print concated arrayprint(randomNumbers.mkString(" "))print("\n"+fruits.mkString(" "))print("\n"+cgpas.mkString(" "))}
concat()
method. Then, we store the returned results in a new array variable.mkString()
method. Then, we print the strings to the console.