本文介绍了带有 `==` 运算符的 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 预发布版.
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.
这与Java的数组相等性一致,使用Object.Equals
,所以比较引用.
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 类型的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!