Write a Scala program to create a new array taking the middle element from three arrays of length 5.
object Scala_Array { def test(x: Array[Int], y: Array[Int], z: Array[Int]): Array[Int] = { if (x.length != 5 || y.length != 5 || z.length != 5) throw new IllegalArgumentException("Array length not matched!") else Array(x(2), y(2), z(2)) } def main(args: Array[String]): Unit = { var result1 = test(Array(1,2,3,4,5),Array(2,3,4,5,6),Array(3,4,5,6,7)) // Print all the array elements println("New array:") for ( x <- result1 ) { print(s"${x}, ") } } }
Sample Output:
New array: 3, 4, 5,
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Sample Output:
need an explanation for this answer? contact us directly to get an explanation for this answer