如何在Scala中创建元组数组?

huangapple go评论119阅读模式
英文:

How to create an Array for Tuples in Scala?

问题

I'm trying to create an empty Array to store coordinates of an object in a Tuple which is then stored in an Array.

When I try:

var walls = Array[Tuple2]()

I'm getting this error message:

kinds of the type arguments (Tuple2) do not conform to the expected kinds of the type parameters (type T).
[error] Tuple2's type parameters do not match type T's expected parameters:
[error] class Tuple2 has two type parameters, but type T has none
[error]         var walls = Array[Tuple2]()

Is there any possibility to do this?

英文:

Im trying to create an empty Array to store coordinates of an object in an Tuple which is then stored in an Array.

When I try:

var walls = Array[Tuple2]()

Im getting this error message:

kinds of the type arguments (Tuple2) do not conform to the expected kinds of the type parameters (type T).
[error] Tuple2's type parameters do not match type T's expected parameters:
[error] class Tuple2 has two type parameters, but type T has none
[error]         var walls = Array[Tuple2]()

Is there any possibility to do this?

答案1

得分: 4

Tuple2 是一种类型构造器(具有 [*, *] => * 类别)。

Array 也是一种类型构造器(具有 [*] => * 类别)。

要使其能够作为 Array 的参数,您需要将 Tuple2 应用于两种类型(具有 * 类别)。

这就是为什么 Array[(Int, Int)],也称为 Array[Tuple2[Int, Int]],有效,而 Array[Tuple2] 则无效的原因。

英文:

Tuple2 is a type constructor (of kind [*, *] => *).

Array is a type constructor too (of kind [*] => *).

You have to apply Tuple2 to two types (of kind *) in order to make it suitable as an argument of Array.

That's why Array[(Int, Int)] aka Array[Tuple2[Int, Int]] is working while Array[Tuple2] is not.

答案2

得分: 2

好的,我找到了一种方法:

var walls = Array<(Int, Int)>()
英文:

Okay I found a way:

var walls = Array[(Int, Int)]()

huangapple
  • 本文由 发表于 2023年2月18日 22:58:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75494182.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定