Scala 数组构造函数?
scala> val a = Array [Double] (10)
a: Array[Double] = Array(10.0)
scala> val a = new Array [Double] (10)
a: Array[Double] = Array(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0)
为什么这两个表达式有不同的语义?
scala> val a = Array [Double] (10)
a: Array[Double] = Array(10.0)
scala> val a = new Array [Double] (10)
a: Array[Double] = Array(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0)
Why these two expressions have different semantics?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这有点令人困惑,但 Scala 有“类”和“对象”的概念,您可以创建类的实例,而“对象”基本上是类的单例实例。它还具有伴生类的概念,它是一对同名的类和对象。这种机制允许“类”本质上具有静态方法,而这在 Scala 中是不可能的。
Array
有一个类和一个伴生对象。此外,Array
对象< /a> 有一个apply
方法。apply
表示您可以使用Array(arg)
创建对象。但由于 Array 是一个伴生类,因此它还有一个构造函数,可以通过更常用的 new Array(arg) 机制进行调用。问题是
Array
对象中的apply
与Array
构造函数具有不同的语义。apply
方法从指定对象中创建一个数组,因此,例如Array(1,2,3)
返回一个由对象1 组成的数组
、2
和3
。另一方面,构造函数采用指定数组维度大小的参数(以便您可以创建多维数组),然后将所有槽初始化为默认值。因此,基本上:
val a = Array [Double] (10)
调用Array
object 上的apply
方法,它创建一个包含给定对象的新数组。val a = new Array [Double] (10)
调用Array
构造函数,该构造函数创建一个具有 10 个槽的新数组,所有槽均初始化为默认值0.0
。It's a bit confusing, but Scala has the notion of classes which you can create instances of, and objects, which are basically singleton instances of a class. It also has the notion of companion classes, which is a pair of a class and an object with the same name. This mechanism allows a "class" to essentially have static methods, which are otherwise not possible in Scala.
Array
has both a class and a companion object. Furthermore, theArray
object has anapply
method.apply
means you can create an object withArray(arg)
. But becauseArray
is a companion class, it also has a constructor that can be called via the more usual mechanism ofnew Array(arg)
.The issue is that
apply
in the theArray
object has different semantics than theArray
constructors. Theapply
method creates an array out of the specified objects, so, for example,Array(1,2,3)
returns an array consisting of the objects1
,2
, and3
. The constructors, on the other hand, take arguments that specify the size of the dimensions of the array (so you can create multidimensional arrays), and then initialize all slots to a default value.So, basically:
val a = Array [Double] (10)
calls theapply
method on theArray
object, which creates a new array containing the given objects.val a = new Array [Double] (10)
calls theArray
constructor, which creates a new array with 10 slots, all initialized to a default value of0.0
.new Array[Double](10)
应该等同于 Java 中的new double[10]
。但 Scala 还在与其集合类对应的单例上提供了便捷的方法,
Array
也不例外。因此,如果您可以说
List(1,2,3,4,5)
,那么您也可以自然地说Array(1,2,3,4,5)
代码>.你可以。但它确实让人们处于一种稍微尴尬的境地,即根据是否添加“new”一词而得到相当不同的结果。考虑到相互竞争的利益,我认为这是总体上最好的解决方案,但确实需要一些时间来适应。
new Array[Double](10)
is supposed to be equivalent tonew double[10]
in Java.But Scala also provides convenience methods on the singletons corresponding to its collection classes, and
Array
is no exception.Thus, if you can say
List(1,2,3,4,5)
it seems natural that you could also sayArray(1,2,3,4,5)
. And you can.But it does leave one in the slightly awkward position of having rather different results depending on whether one adds the word
new
or not. Given the competing interests, I think it's the best solution overall, but it does take a little getting used to.