Scala 中的模式匹配

16 阅读2分钟

分析问题并解决

以数字求籍贯

选择结构if语句:

object matchcase1 {
  // 42 -> 湖北省
  // 53 -> 四川省
  def main(args: Array[String]): Unit = {
    // 选择结构if语句
    val code = 42
    val province = ""
    if(code == 42) {
      province = "湖北省"
    } else if(code == 52) {
      province = "四川省"
    } else{
      province = "未知"
    }
    println(province)
  }
}

模式匹配:

object matchcase1 {
  // 42 -> 湖北省
  // 53 -> 四川省
  def main(args: Array[String]): Unit = {
    // 模式匹配
    val code = 52
    val province = code match{
      case 42 => "湖北省"
      case 52 => "四川省"
      case _ => "未知"
    }
    println(province)
  }
}

match case的基本格式

  1. 如果能精准匹配成功,就会执行后面的代码
  2. case _ 它表示可以匹配任意的内容。一定要放到match case的最后一条
  3. case _ 不能省略:如果匹配不成功,就会报错
object matchcase2 {

  def main(args: Array[String]): Unit = {
    // 输入1,输出单词one
    // 输入2,输出单词two
    // 输入3,输出单词three
    // 输入其他,输出单词other

    // 1. 从键盘获取数字
    val code = 1
    // 2. 使用match case进行匹配
    code match {
      case 1 => println("one")
      case 2 => println("two")
      case _ => println("未知")
    }

  }
}

match case的精准匹配

  1. 匹配精确值
  2. 匹配数据类型
object matchcase3 {
  def proccessValue(value:Any): Unit = {
    value match {
      case x: Int => println("Int")
      case x: String => println("String")
      case _ => println("other")
    }
  }
  def main(args: Array[String]): Unit = {
    val obj = 10
    proccessValue(obj)
  }
}
  1. 匹配元素的个数
object matchcase3 {
  def processNum(value:Any): Unit = {
    value match {
      case (a,b,c) => println(s"三个元素的元组,第一个元素是${a}")
      case (x,y) => println("两个元素的元组")
      case _ => println("其他")
    }
  }

  def main(args: Array[String]): Unit = {
    processNum((1,2))
    processNum((1,2,3))
  }
}
  1. 匹配数组元素个数,元素特征
object matchcase4 {
  def matchArray(arr:Any): Unit = {
    arr match {
      case Array(x,y) => println("匹配到有两个元素的数组", x ,y)
      case Array(1,b,c) => println("匹配到有三个元素的数组,第一个元素是1")
      case Array(a,b,c) => println("匹配到有三个元素的数组", a, b, c)
      case Array(10, _*) => println("第一个元素是10,元素个数有任意个")
      case _ => println("没有匹配到")
    }
  }

  def main(args: Array[String]): Unit = {
    val arr1 = Array(1,2,3)
    val arr2 = Array(2,2,3)
    val arr3 = Array(10,2,3,1)
    matchArray(arr1)
    matchArray(arr2)
    matchArray(arr3)
  }
}
  1. 匹配案例类
object matchcase5 {
  case class Circle(r: Double)
  case class Rectangle(w: Double, h:Double)

  def calculateArea(shape:Any): Double = {
    shape match {
      case Circle(r) => 3.14 * r * r
      case Rectangle(w,h) => w * h
      case _ => 0.0
    }
  }

  def main(args: Array[String]): Unit = {
   val circle = Circle(2.0)
   val rectangle = Rectangle(2.0, 3.0)
   println(calculateArea(circle))
   println(calculateArea(rectangle))
  }
}
  1. 变量声明时的匹配
object matchcase6 {

  def main(args:Array[String]): Unit = {
    val arr = Array(11, 22, 33, 4, 5)
    //定义3个变量:x,y,z 保存数组中的前三个元素的值
//    val x = arr(0)
//    val y = arr(1)
//    val z = arr(2)
//
    val Array(x,y,z, _*) = arr
    println(x, y, z)
  }
}