Scala 数组构造函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2700175/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Scala Array constructor?
提问by ?ukasz Lew
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?
为什么这两个表达式有不同的语义?
回答by mipadi
It's a bit confusing, but Scala has the notion of classeswhich 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.
这有点令人困惑,但 Scala 有类的概念,您可以为其创建实例,而对象则基本上是类的单例实例。它也有同伴类的概念,它是一对同名的类和对象。这种机制允许“类”本质上具有静态方法,否则在 Scala 中是不可能的。
Arrayhas both a classand a companion object. Furthermore, the Arrayobjecthas an applymethod. applymeans you can create an object with Array(arg). But because Arrayis a companion class, it also has a constructor that can be called via the more usual mechanism of new Array(arg).
Array有一个类和一个伴生对象。此外,Array对象有一个apply方法。apply意味着您可以使用Array(arg). 但是因为Array是一个伴生类,它也有一个构造函数,可以通过更常见的new Array(arg).
The issue is that applyin the the Arrayobject has different semantics than the Arrayconstructors. The applymethod creates an array out of the specified objects, so, for example, Array(1,2,3)returns an array consisting of the objects 1, 2, and 3. 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.
问题是,apply在该Array物体具有比不同语义的Array构造函数。该apply方法创建一个数组出指定的对象的,所以,例如,Array(1,2,3)返回一个由对象的数组1,2和3。另一方面,构造函数采用指定数组维度大小的参数(因此您可以创建多维数组),然后将所有槽初始化为默认值。
So, basically:
所以,基本上:
val a = Array [Double] (10)calls theapplymethod on theArrayobject, which creates a new array containing the given objects.val a = new Array [Double] (10)calls theArrayconstructor, which creates a new array with 10 slots, all initialized to a default value of0.0.
val a = Array [Double] (10)调用objectapply上的方法,该方法创建一个包含给定对象的新数组。Arrayval a = new Array [Double] (10)调用Array构造函数,它创建一个具有 10 个插槽的新数组,所有数组都初始化为默认值0.0。
回答by Rex Kerr
new Array[Double](10)is supposed to be equivalent to new double[10]in Java.
new Array[Double](10)应该相当于new double[10]在Java中。
But Scala also provides convenience methods on the singletons corresponding to its collection classes, and Arrayis no exception.
但是 Scala 也为与其集合类对应的单例提供了便捷的方法,Array也不例外。
Thus, if you can say List(1,2,3,4,5)it seems natural that you could also say Array(1,2,3,4,5). And you can.
因此,如果你可以说List(1,2,3,4,5),你也可以说Array(1,2,3,4,5). 你可以。
But it does leave one in the slightly awkward position of having rather different results depending on whether one adds the word newor not. Given the competing interests, I think it's the best solution overall, but it does take a little getting used to.
但它确实让一个人处于略为尴尬的境地,即根据是否添加单词而产生相当不同的结果new。考虑到相互竞争的利益,我认为这是总体上最好的解决方案,但确实需要一点时间来适应。

