Q:

Write a Scala program to test the equality of two arrays

0

Write a Scala program to test the equality of two arrays.

All Answers

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

object scala_basic {  
   
   def test(my_array1: Array[Int], my_array2: Array[Int]) : Boolean = {
     var equalOrNot = true;
     if(my_array1.length == my_array2.length)
      {
      for (i <- 0 to  my_array1.length-1) 
         {
          if(my_array1(i) != my_array2(i))
           {
             equalOrNot = false;             
           }
        }
      }
      else
      {
        equalOrNot  = false;
      }  
      if  (equalOrNot) true
      else false
     }         
     def main(args: Array[String]): Unit = {  
       //Call the following Java class for some array operation
       import java.util.Arrays;
       var arr1 =  Array(2, 5, 7, 9, 11);
       var arr2 =  Array(2, 5, 7, 9, 11);
       println("Original Array1 : "+Arrays.toString(arr1));
       println("Original Array2 : "+Arrays.toString(arr2));     
       println("Whether the said two arrays are equal?")
       println(test(arr1,arr2))       
       var arr3 =  Array(2, 5, 7, 9, 11);
       var arr4 =  Array(2, 5, 7, 9, 10);
       println("Original Array1 : "+Arrays.toString(arr3));
       println("Original Array2 : "+Arrays.toString(arr4)); 
       println("Whether the said two arrays are equal?")
       println(test(arr3,arr4))
    }
}

Sample Output:

Original Array1 : [2, 5, 7, 9, 11]
Original Array2 : [2, 5, 7, 9, 11]
Whether the said two arrays are equal?
true
Original Array1 : [2, 5, 7, 9, 11]
Original Array2 : [2, 5, 7, 9, 10]
Whether the said two arrays are equal?
false

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