带有==运算符的Array类型的奇怪行为 [英] Strange behaviour of the Array type with `==` operator
本文介绍了带有==运算符的Array类型的奇怪行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
scala> List(1,2,3) == List(1,2,3)
res2: Boolean = true
scala> Map(1 -> "Olle") == Map(1 -> "Olle")
res3: Boolean = true
但是当尝试对Array做同样的事情时,它的工作原理却不一样.为什么?
But when trying to do the same with Array, it does not work the same. Why?
scala> Array('a','b') == Array('a','b')
res4: Boolean = false
我使用了2.8.0.RC7和2.8.0.Beta1-prerelease.
I have used 2.8.0.RC7 and 2.8.0.Beta1-prerelease.
推荐答案
因为数组的等于"的定义是它们指向同一数组.
Because the definition of "equals" for Arrays is that they refer to the same array.
这与使用Object.Equals
的Java数组相等性保持一致,因此它比较引用.
This is consistent with Java's array equality, using Object.Equals
, so it compares references.
If you want to check pairwise elements, then use sameElements
Array('a','b').sameElements(Array('a','b'))
或deepEquals
(在2.8中已弃用),因此请使用:
or deepEquals
, which has been deprecated in 2.8, so instead use:
Array('a','b').deep.equals(Array('a','b').deep)
这篇关于带有==运算符的Array类型的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文