0

我正在尝试用不同的地图创建地图

val mymap = Map("name"->"somename",Map(1->2))

我从编译器得到:

scala: type mismatch;
 found   : scala.collection.immutable.Map[Int,Int]
 required: (?, ?)
  val mymap = Map("name"->"somename",Map(1->2))
                                        ^
4

2 回答 2

4

Why do you expect it to work? You've provided only key without value:

val key = Map(1->2)
val mymap = Map("name"->"somename", key)

Perhaps you wanted to combine two maps? This can be done with:

val mymap = Map("name"->"somename") ++ Map(1->2)
// scala.collection.immutable.Map[Any,Any] = Map(name -> somename, 1 -> 2)
于 2013-07-11T15:27:51.543 回答
2

Map 由键值对(其类型为(?, ?))组成。您还必须将 Map 值分配给一个键:

val mymap = Map("name"->"somename","othername"->Map(1->2))
于 2013-07-11T15:28:41.920 回答