为什么' Array(0,1,2) = Array(0,1,2) '不返回预期结果?

时间:2021-06-15 23:00:40

As far as I understand, Scala's == defines the natural equality of two objects.

据我所知,Scala的==定义了两个对象的自然等式。

I expected that Array(0,1,2) == Array(0,1,2) compares the natural equality. For example, checks if all elements of the array return true when compared with the corresponding elements of the other array.

我期望数组(0,1,2)=数组(0,1,2)比较自然等式。例如,检查数组的所有元素与其他数组的相应元素相比是否返回true。

People told me that Scala's Array is just a Java [] which only compares identity. Wouldn't it be more meaningful to override Array'sequals method to compare natural equality instead?

人们告诉我Scala的数组只是一个Java[],它只比较身份。重写数组的顺序方法来比较自然等式不是更有意义吗?

3 个解决方案

#1


19  

Scala 2.7 tried to add functionality to Java [] arrays, and ran into corner cases that were problematic. Scala 2.8 has declared that Array[T] is T[], but it provides wrappers and equivalents.

Scala 2.7尝试向Java[]数组添加功能,遇到了一些问题。Scala 2.8已经声明数组[T]是T[],但是它提供了包装器和等价物。

Try the following in 2.8 (edit/note: as of RC3, GenericArray is ArraySeq--thanks to retronym for pointing this out):

在2.8(编辑/注意:在RC3中,GenericArray是ArraySeq——感谢retronym指出这一点):

import scala.collection.mutable.{GenericArray=>GArray, WrappedArray=>WArray}
scala> GArray(0,1,2) == GArray(0,1,2)
res0: Boolean = true

scala> (Array(0,1,2):WArray[Int]) == (Array(0,1,2):WArray[Int])
res1: Boolean = true

GenericArray acts just like Array, except with all the Scala collections goodies added in. WrappedArray wraps Java [] array; above, I've cast a plain array to it (easier than calling the implicit conversion function) and then compared the wrapped arrays. These wrappers, though backed by a [] array, also give you all the collection goodies.

泛型数组的作用就像数组一样,只不过加入了所有Scala集合。封装Java[]阵列;上面,我向它转换了一个普通数组(比调用隐式转换函数更容易),然后比较了封装的数组。这些包装器虽然有[]数组作为支持,但也为您提供了所有集合的好处。

#2


8  

Scala doesn't override Array's equality because it's not possible. One can only override methods when subclassing. Since Array isn't being subclassed (which isn't possible), Scala cannot override its methods.

Scala不重写数组的等式,因为这是不可能的。只能在子类化时重写方法。由于数组没有被子类化(这是不可能的),Scala无法覆盖它的方法。

#3


5  

But Scala's String is also just a Java String but Scala overrides equals to compare natural equality.

但是Scala的字符串也是一个Java字符串,但是Scala重写等于比较自然的相等。

Scala doesn't override anything there; java.lang.String has a value-dependant implementation of equals() (like many other Java classes, but unlike arrays).

Scala没有覆盖任何东西;. lang。String具有与值相关的equals()实现(与许多其他Java类一样,但与数组不同)。

#1


19  

Scala 2.7 tried to add functionality to Java [] arrays, and ran into corner cases that were problematic. Scala 2.8 has declared that Array[T] is T[], but it provides wrappers and equivalents.

Scala 2.7尝试向Java[]数组添加功能,遇到了一些问题。Scala 2.8已经声明数组[T]是T[],但是它提供了包装器和等价物。

Try the following in 2.8 (edit/note: as of RC3, GenericArray is ArraySeq--thanks to retronym for pointing this out):

在2.8(编辑/注意:在RC3中,GenericArray是ArraySeq——感谢retronym指出这一点):

import scala.collection.mutable.{GenericArray=>GArray, WrappedArray=>WArray}
scala> GArray(0,1,2) == GArray(0,1,2)
res0: Boolean = true

scala> (Array(0,1,2):WArray[Int]) == (Array(0,1,2):WArray[Int])
res1: Boolean = true

GenericArray acts just like Array, except with all the Scala collections goodies added in. WrappedArray wraps Java [] array; above, I've cast a plain array to it (easier than calling the implicit conversion function) and then compared the wrapped arrays. These wrappers, though backed by a [] array, also give you all the collection goodies.

泛型数组的作用就像数组一样,只不过加入了所有Scala集合。封装Java[]阵列;上面,我向它转换了一个普通数组(比调用隐式转换函数更容易),然后比较了封装的数组。这些包装器虽然有[]数组作为支持,但也为您提供了所有集合的好处。

#2


8  

Scala doesn't override Array's equality because it's not possible. One can only override methods when subclassing. Since Array isn't being subclassed (which isn't possible), Scala cannot override its methods.

Scala不重写数组的等式,因为这是不可能的。只能在子类化时重写方法。由于数组没有被子类化(这是不可能的),Scala无法覆盖它的方法。

#3


5  

But Scala's String is also just a Java String but Scala overrides equals to compare natural equality.

但是Scala的字符串也是一个Java字符串,但是Scala重写等于比较自然的相等。

Scala doesn't override anything there; java.lang.String has a value-dependant implementation of equals() (like many other Java classes, but unlike arrays).

Scala没有覆盖任何东西;. lang。String具有与值相关的equals()实现(与许多其他Java类一样,但与数组不同)。