scala.collection.mutable.Map。+ =不起作用?

时间:2014-05-28 15:57:24

标签: android scala

我在Android中使用Scala,这是测试代码之一:

  def bookList() = {
    val res = resource.getAssets.open("demo.png")
    val image = Drawable.createFromStream(res, "demo.png")
    val map = Map[String, Drawable]()
    for (i <- 1 to 100) {
      map += ("test book" -> image)
    }
    map
  }

但是bookList方法的返回结果总是得到一个大小为1的映射。所以我很困惑,是否有任何可能的原因导致这种错误的行为?

1 个答案:

答案 0 :(得分:3)

试试这个

def bookList() = {
  val res = resource.getAssets.open("demo.png")
  val image = Drawable.createFromStream(res, "demo.png")
  val map = Map[String, Drawable]()
  for (i <- 1 to 100) {
    map += (s"test book$i" -> image)
  }
  map
}