Scala 数组构造函数?

发布于 2024-08-29 20:35:39 字数 236 浏览 4 评论 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)

为什么这两个表达式有不同的语义?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

2024-09-05 20:35:39

这有点令人困惑,但 Scala 有“类”和“对象”的概念,您可以创建类的实例,而“对象”基本上是类的单例实例。它还具有伴生类的概念,它是一对同名的类和对象。这种机制允许“类”本质上具有静态方法,而这在 Scala 中是不可能的。

Array 有一个和一个伴生对象。此外, Array 对象< /a> 有一个 apply 方法。 apply 表示您可以使用 Array(arg) 创建对象。但由于 Array 是一个伴生类,因此它还有一个构造函数,可以通过更常用的 new Array(arg) 机制进行调用。

问题是 Array 对象中的 applyArray 构造函数具有不同的语义。 apply 方法从指定对象中创建一个数组,因此,例如 Array(1,2,3) 返回一个由对象 1 组成的数组23。另一方面,构造函数采用指定数组维度大小的参数(以便您可以创建多维数组),然后将所有槽初始化为默认值。

因此,基本上:

  • 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, the Array object has an apply method. apply means you can create an object with Array(arg). But because Array is a companion class, it also has a constructor that can be called via the more usual mechanism of new Array(arg).

The issue is that apply in the the Array object has different semantics than the Array constructors. The apply method 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.

So, basically:

  • val a = Array [Double] (10) calls the apply method on the Array object, which creates a new array containing the given objects.
  • val a = new Array [Double] (10) calls the Array constructor, which creates a new array with 10 slots, all initialized to a default value of 0.0.
耳根太软 2024-09-05 20:35:39

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 to new 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 say Array(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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文