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?
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.
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.
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.
From Programming Scala:
Array(1,2,4,5).sameElements(Array(1,2,4,5))
You need to change your last line to
a.deep == b.deep
to do a deep comparison of the arrays.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With