基本语法

模式匹配语法中,采用match关键字声明,每个分支采用case关键字进行声明,当需要匹配时,会从第一个case分支开始,如果匹配成功,那么执行对应的逻辑代码,如果匹配不成功,继续执行下一个分支进行判断。如果所有case都不匹配,那么会执行case _分支,类似于Java中default语句

  1. public static void main(String[] args) {
  2. int i = 10;
  3. switch (i){
  4. case 10:
  5. System.out.println("10");
  6. break;
  7. case 20:
  8. System.out.println("20");
  9. break;
  10. case 30:
  11. System.out.println("30");
  12. break;
  13. }
  14. }
  1. object test {
  2. def main(args: Array[String]): Unit = {
  3. var a = 10
  4. var b = 20
  5. var operator = 'd'
  6. var result = operator match{
  7. case '+' => a + b
  8. case '-' => a - b
  9. case '*' => a * b
  10. case '/' => a / b
  11. case _ => "illegal"
  12. }
  13. println(result) //illegal
  14. }
  15. }
  1. 如果所有case都不匹配,那么会执行case 分支,类似于Java中default语句,若此时没有case 分支,那么会抛出MatchError。
  2. 每个case中,不需要使用break语句,自动中断case。
  3. match case语句可以匹配任何类型,而不只是字面量。
  4. => 后面的代码块,直到下一个case语句之前的代码是作为一个整体执行,可以使用{}括起来,也可以不括

    模式守卫

    如果想要表达匹配某个范围的数据,就需要在模式匹配中增加条件守卫。

    1. object test {
    2. def main(args: Array[String]): Unit = {
    3. def abs(x:Int) = x match {
    4. case i:Int if i >= 0 => i
    5. case i:Int if i <= 0 => -i
    6. case _ => "illegal"
    7. }
    8. println(abs(-5)) //5
    9. println(abs(5)) //5
    10. }
    11. }

    模式匹配类型

    匹配常量

    1. object test {
    2. def main(args: Array[String]): Unit = {
    3. def describeConst(x:Any) = x match {
    4. case 1 => "Int one"
    5. case "hello" => "String hello"
    6. case true => "Boolean true"
    7. case '+' => "Char +"
    8. case _ => "illegal"
    9. }
    10. println(describeConst("hello")) //String hello
    11. println(describeConst('+')) //Char +
    12. println(describeConst(0.3)) //illegal
    13. }
    14. }

    匹配类型

    1. object test {
    2. def main(args: Array[String]): Unit = {
    3. def describeType(x:Any) = x match {
    4. case i:Int => "Int " + i
    5. case s:String => "String " + s
    6. case list:List[String] => "List " + list
    7. case array: Array[Int] => "Array[Int] " + array.mkString(",")
    8. case a => "Something else:" + a
    9. }
    10. println(describeType(35)) //Int 35
    11. println(describeType("hello")) //String hello
    12. println(describeType(List("hello","scala"))) //List List(hello, scala)
    13. println(describeType(List(1,2,3))) //List List(1, 2, 3)
    14. println(describeType(Array("hello","scala"))) //Something else:[Ljava.lang.String;@75bd9247
    15. println(describeType(Array(1,2,3))) //Array[Int] 1,2,3
    16. }
    17. }

    匹配数组

    1. object test {
    2. def main(args: Array[String]): Unit = {
    3. for (arr <- List(
    4. Array(0),//0
    5. Array(1, 0),//Array(1,0)
    6. Array(0, 1, 0),//以0开头的数组
    7. Array(1, 1, 0),//中间为1的三元素数组
    8. Array(2, 3, 7, 15),//something else
    9. Array("hello", 20, 30),//something else
    10. )) {
    11. val result = arr match {
    12. case Array(0) => "0"
    13. case Array(1, 0) => "Array(1,0)"
    14. case Array(x, y) => "Array: " + x + "," + y
    15. case Array(0, _*) => "以0开头的数组"
    16. case Array(x, 1, z) => "中间为1的三元素数组"
    17. case _ => "something else"
    18. }
    19. println(result)
    20. }
    21. }
    22. }

    匹配列表

    1. object test {
    2. def main(args: Array[String]): Unit = {
    3. for (list <- List(
    4. List(0), //0
    5. List(1,0), //List(x,y):1,0
    6. List(0,0,0), //List(0,....)
    7. List(1,1,0), //List(0,....)
    8. List(88) //List(0,....)
    9. )){
    10. val result = list match {
    11. case List(0) => "0"
    12. case List(x,y) => "List(x,y):" + x + ',' + y
    13. case List(x,_*) => "List(0,....)"
    14. case List(a) => "List(a):" + a
    15. case _ => "something else"
    16. }
    17. println(result)
    18. }
    19. val list = List(1,5,7,9,24) //first:1,second:5,rest:List(7, 9, 24)
    20. val list1 = List(1) //something else
    21. list match {
    22. case first :: second :: rest => println(s"first:$first,second:$second,rest:$rest")
    23. case _ => println("something else")
    24. }
    25. list1 match {
    26. case first :: second :: rest => println(s"first:$first,second:$second,rest:$rest")
    27. case _ => println("something else")
    28. }
    29. }
    30. }

    匹配元组

    1. object test {
    2. def main(args: Array[String]): Unit = {
    3. for (tuple <- List(
    4. (0, 1), //0,1
    5. (0, 0), //0,0
    6. (0, 1, 0), //(a,1,_) 0
    7. (0, 1, 1), //(a,1,_) 0
    8. (1, 23, 56), //(x,y,z):12356
    9. ("hello", true, 0.5), //(x,y,z):hellotrue0.5
    10. )) {
    11. val result = tuple match {
    12. case (a, b) => "" + a + "," + b
    13. case (0, _) => "(0,_)"
    14. case (a, 1, _) => "(a,1,_) " + a
    15. case (x, y, z) => "(x,y,z):" + x + "" + y + "" + z
    16. case _ => "something else"
    17. }
    18. println(result)
    19. }
    20. }
    21. }