[英]How to return Map in scala
I just started playing with scala and i cross the following issue.我刚开始玩 Scala,我遇到了以下问题。 I want to simply return a Map with Int as key and List of Tuples for values.
我想简单地返回一个以 Int 为键的 Map 和值的元组列表。 That is my method:
这是我的方法:
def findOpenTiles(board: Array[Array[Int]]): Map[Int, List[(Int, Int)]]={
val openTilesMap = Map[Int, List[(Int, Int)]]
for (x <- 0 until Constant.boardWidth; y <- 0 until Constant.boardHeight) yield {
if (hasOpenTile(board, x, y)){
// add to values to openTilesMap
}
}
openTilesMap
}
However my IDE shows error as:但是我的 IDE 显示错误为:
Expression of type (Seq[(Int, List[Int, Int])]) => Map[Int, List[(Int, Int)]] doesn't conform to expected type Map[Int, List[(Int, Int)]]
Does it mean that val openTilesMap = Map[Int, List[(Int, Int)]]
creates Seq of Tuples (Int, List[Int, Int])
instead of Map?这是否意味着
val openTilesMap = Map[Int, List[(Int, Int)]]
创建Seq of Tuples (Int, List[Int, Int])
而不是 Map? If so, how can i make it return Map?如果是这样,我怎样才能让它返回 Map?
// edit // 编辑
I'm trying to write a bot to javascript game.我正在尝试为 javascript 游戏编写一个机器人。 I'm mapping a board of tiles.
我正在绘制一块瓷砖。 In the mentioned method I am trying to find all "open tiles" (tiles which are not fully surounded by other tiles, thus can be moved) and in the return i would like to have a Map where key is a tile number with coordinates as values.
在提到的方法中,我试图找到所有“开放的瓷砖”(没有被其他瓷砖完全包围的瓷砖,因此可以移动),作为回报,我想要一个地图,其中键是一个带有坐标的瓷砖编号值。 In next step i want to find if it is possible to find path between "open" tiles with the same number.
在下一步中,我想找到是否可以找到具有相同编号的“开放”图块之间的路径。
I think the problem is the line我认为问题是线路
val openTilesMap = Map[Int, List[(Int, Int)]]
You should try this:你应该试试这个:
val openTilesMap: Map[Int, List[(Int, Int)]] = Map()
Your version assigns the type Map[Int, List[(Int, Int)]]
to the value openTilesMap
.您的版本将类型
Map[Int, List[(Int, Int)]]
分配给值openTilesMap
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.