如何在 Scala 中定义列表列表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1350453/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 01:38:09  来源:igfitidea点击:

How to define a list of lists in Scala?

scala

提问by

I would like to create a storage for the following type:

我想为以下类型创建一个存储:

List(List(2.3,1.1),List(2.2, 1))

But if I do the following:

但是,如果我执行以下操作:

var y = List(List (1.0, 2.2), List(2, 1.1, -2.1))

then it creates as List[AnyVal] and gives me error if I try to perform math operation:

然后它创建为 List[AnyVal] 并在我尝试执行数学运算时给我错误:

y(0)(0) * 2         // Error - value '2' is not a member of AnyVal

回答by Sandor Murakozi

In both of your examples one list contains one number that is an Int (last 1 in the first case and 2 as the first element of the second list), the rest of the numbers are Doubles. Therefore the inferred type of the list elements will be AnyVal, which is the first common supertype of them, so your outer list will become List[List[AnyVal]].

在您的两个示例中,一个列表包含一个 Int 数字(第一种情况下为最后一个 1,第二个列表的第一个元素为 2),其余数字为双精度数。因此,列表元素的推断类型将是 AnyVal,这是它们的第一个公共超类型,因此您的外部列表将成为 List[List[AnyVal]]。

If you also try it with scala 2.8 then it should use Numeric instead of AnyVal, as it became the supertype of both Double and Int. Most numeric operations (multiplication in your case) will also work on them.

如果您也尝试使用 Scala 2.8,那么它应该使用 Numeric 而不是 AnyVal,因为它成为 Double 和 Int 的超类型。大多数数字运算(在您的情况下为乘法)也适用于它们。

To fix your problem with 2.7.x simply use Doubles for these values (1.0 or 1D).

要解决 2.7.x 的问题,只需对这些值(1.0 或 1D)使用 Doubles。

Explicitly declaring the type as List[List[Double]] will probably also help. In this case the Int values you gave will be converted to Doubles.

明确地将类型声明为 List[List[Double]] 可能也会有所帮助。在这种情况下,您提供的 Int 值将转换为 Doubles。

回答by James Black

You may find this link useful: http://debasishg.blogspot.com/2009/01/higher-order-abstractions-in-scala-with.html

您可能会发现此链接很有用:http: //debasishg.blogspot.com/2009/01/higher-order-abstractions-in-scala-with.html

From the link above here is an example of making a list of lists:

从上面的链接这里是制作列表列表的示例:

val brs: List[List[String]] =
  List(List("dave", "john", "sam"), List("peter", "robin", "david"), List("pras", "srim"))

So, for your problem you may want to have:

因此,对于您的问题,您可能想要:

var y : List[List[Float]] = ...

This way you remove any problems with type inference.

通过这种方式,您可以消除类型推断的任何问题。

回答by VonC

Couple of remarks:

几点说明:

F:\prog\scala\scala-2.8.0.r18341-b20090718020201\bin>scala
Welcome to Scala version 2.8.0.r18341-b20090718020201 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_13).
Type in expressions to have them evaluated.
Type :help for more information.

scala> var z = List(List (1.0, 2.2), List(2, 1.1, -2.1))
z: List[List[AnyVal]] = List(List(1.0, 2.2), List(2, 1.1, -2.1))

scala> var z = List(List (1.0f, 2.2f), List(2f, 1.1f, -2.1f))
z: List[List[Float]] = List(List(1.0, 2.2), List(2.0, 1.1, -2.1))

Meaning, like the question "Java: my method wants the double type instead of float?":

意思是,就像问题“ Java:我的方法想要双精度型而不是浮点型?”:

The 'f' at the end of the number makes it a float instead of a double.
Java won't automatically narrow a double to a float.

数字末尾的“f”使其成为浮点数而不是双精度数。
Java 不会自动将双精度数缩小为浮点数。



scala> var z = (1.0f :: 2.2f :: Nil) :: (2f :: 1.1f :: -2.1f :: Nil) :: Nil
z: List[List[Float]] = List(List(1.0, 2.2), List(2.0, 1.1, -2.1))

works too

也能用



Just adding the explicit type would not be enough:

仅仅添加显式类型是不够的:

scala> var z2 : List[List[Float]] = List(List(1.0, 2.2), List(2, 1.1, -2.1))
<console>:4: error: type mismatch;
 found   : Double(1.0)
 required: Float
       var z2 : List[List[Float]] = List(List(1.0, 2.2), List(2, 1.1, -2.1))
                                              ^
<console>:4: error: type mismatch;
 found   : Double(2.2)
 required: Float
       var z2 : List[List[Float]] = List(List(1.0, 2.2), List(2, 1.1, -2.1))
                                                   ^
<console>:4: error: type mismatch;
 found   : Double(1.1)
 required: Float
       var z2 : List[List[Float]] = List(List(1.0, 2.2), List(2, 1.1, -2.1))
                                                                 ^
<console>:4: error: type mismatch;
 found   : Double(-2.1)
 required: Float
       var z2 : List[List[Float]] = List(List(1.0, 2.2), List(2, 1.1, -2.1))
                                                                      ^

That is the same with one single variable:

这与单个变量相同:

scala> var f : Float = -2.1
<console>:4: error: type mismatch;
 found   : Double(-2.1)
 required: Float
       var f : Float = -2.1
                       ^

scala> var f : Float = -2.1f
f: Float = -2.1

回答by Luigi Plinge

This seems to have been fixed in Scala 2.9

这似乎已在 Scala 2.9 中修复

Welcome to Scala version 2.9.0.1 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_22).
Type in expressions to have them evaluated.
Type :help for more information.

scala> var y = List(List (1.0, 2.2), List(2, 1.1, -2.1))
y: List[List[Double]] = List(List(1.0, 2.2), List(2.0, 1.1, -2.1))

scala> y(0)(0) * 2
res0: Double = 2.0

回答by chiesa

var y:List[List[Double]] = List(List (1.0, 2.2), List(2, 1.1, -2.1))

y(0)(0)*2