Q:

Write a Scala program to create a new array taking the middle element from three arrays of length 5

0

Write a Scala program to create a new array taking the middle element from three arrays of length 5.

All Answers

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

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,

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

total answers (1)

Similar questions


need a help?


find thousands of online teachers now