Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I compare two arrays in scala?

val a: Array[Int] = Array(1,2,4,5) val b: Array[Int] = Array(1,2,4,5) a==b // false 

Is there a pattern-matching way to see if two arrays (or sequences) are equivalent?

like image 807
Phil H Avatar asked Mar 22 '11 15:03

Phil H


People also ask

Does== work for arrays?

On the other hand, to check if two arrays are equal in terms of their contents, Java provides the Arrays. equals static method. This method will iterate through the arrays, per position in parallel, and apply the == operator, for every pair of elements.

How do you compare two arrays the same?

The Arrays. equals() method checks the equality of the two arrays in terms of size, data, and order of elements. This method will accept the two arrays which need to be compared, and it returns the boolean result true if both the arrays are equal and false if the arrays are not equal.

Can you compare two arrays?

In Java, we can compare two arrays by comparing each element of the array. Java Arrays class provides two predefined methods that is used to compare two arrays in Java. In this section, we will learn how to compare two Arrays using Arrays. equals() method and Arrays.


2 Answers

From Programming Scala:

Array(1,2,4,5).sameElements(Array(1,2,4,5)) 
like image 191
sc_ray Avatar answered Sep 28 '22 15:09

sc_ray


You need to change your last line to

a.deep == b.deep 

to do a deep comparison of the arrays.

like image 26
Moritz Avatar answered Sep 28 '22 14:09

Moritz