Scala 範例程式 - 查詢 (Maps)

原始程式: Maps.scala

/** Maps are easy to use in Scala. */
object Maps {
  val colors = Map("red" -> 0xFF0000,
                   "turquoise" -> 0x00FFFF,
                   "black" -> 0x000000,
                   "orange" -> 0xFF8040,
                   "brown" -> 0x804000)
  def main(args: Array[String]) {
    for (name <- args) println(
      colors.get(name) match {
        case Some(code) =>
          name + " has code: " + code
        case None =>
          "Unknown color: " + name
      }
    )
  }
}

執行結果:

D:\Scala\code>scalac Maps.scala

D:\Scala\code>scala Maps

D:\Scala\code>scala Maps red
red has code: 16711680

D:\Scala\code>scala Maps red brown orange
red has code: 16711680
brown has code: 8404992
orange has code: 16744512
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License