数组类型的奇怪行为 [英] Strange behaviour of the Array type
问题描述
scala> List(1,2,3) == List(1,2,3)
res2: Boolean = true
scala> Map(1 -> "Olle") == Map(1 -> "Olle")
res3: Boolean = true
但尝试做相同的磁盘阵列,它不工作一样。为什么呢?
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- $ P $租赁前。
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.
如果您要检查成对的元素,然后使用 sameElements
If you want to check pairwise elements, then use sameElements
Array('a','b').sameElements(Array('a','b'))
或 deepEquals
,已去$ P $ 2.8 pcated,所以改用:
or deepEquals
, which has been deprecated in 2.8, so instead use:
Array('a','b').deep.equals(Array('a','b').deep)
有一个关于<一个一个很好的Nabble讨论href=\"http://scala-programming-language.1934581.n4.nabble.com/scala-Array-equality-td2001726.html\">array平等。
这篇关于数组类型的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!