英文:
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)]()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论