简体   繁体   English

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

[英]How to define a list of lists in Scala?

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

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. 在两个示例中,一个列表包含一个作为Int的数字(第一个案例中的最后一个,第二个列表中的第一个元素),其余的数字是双打。 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]]. 因此列表元素的推断类型将是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. 如果你也尝试使用scala 2.8,那么它应该使用Numeric而不是AnyVal,因为它成为Double和Int的超类型。 Most numeric operations (multiplication in your case) will also work on them. 大多数数值运算(在你的情况下乘法)也适用于它们。

To fix your problem with 2.7.x simply use Doubles for these values (1.0 or 1D). 要修复2.7.x的问题,只需使用双打来获取这些值(1.0或1D)。

Explicitly declaring the type as List[List[Double]] will probably also help. 明确地将类型声明为List [List [Double]]可能也会有所帮助。 In this case the Int values you gave will be converted to Doubles. 在这种情况下,您给出的Int值将转换为Doubles。

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. 这样您就可以删除类型推断的任何问题。

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:我的方法想要double类型而不是float? ”:

The 'f' at the end of the number makes it a float instead of a double. 数字末尾的'f'使其成为浮点而不是双精度。
Java won't automatically narrow a double to a float. Java不会自动将double缩小为float。


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

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
var y:List[List[Double]] = List(List (1.0, 2.2), List(2, 1.1, -2.1))

y(0)(0)*2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM