基本语法
模式匹配语法中,采用match关键字声明,每个分支采用case关键字进行声明,当需要匹配时,会从第一个case分支开始,如果匹配成功,那么执行对应的逻辑代码,如果匹配不成功,继续执行下一个分支进行判断。如果所有case都不匹配,那么会执行case _分支,类似于Java中default语句
public static void main(String[] args) {int i = 10;switch (i){case 10:System.out.println("10");break;case 20:System.out.println("20");break;case 30:System.out.println("30");break;}}
object test {def main(args: Array[String]): Unit = {var a = 10var b = 20var operator = 'd'var result = operator match{case '+' => a + bcase '-' => a - bcase '*' => a * bcase '/' => a / bcase _ => "illegal"}println(result) //illegal}}
- 如果所有case都不匹配,那么会执行case 分支,类似于Java中default语句,若此时没有case 分支,那么会抛出MatchError。
- 每个case中,不需要使用break语句,自动中断case。
- match case语句可以匹配任何类型,而不只是字面量。
=> 后面的代码块,直到下一个case语句之前的代码是作为一个整体执行,可以使用{}括起来,也可以不括
模式守卫
如果想要表达匹配某个范围的数据,就需要在模式匹配中增加条件守卫。
object test {def main(args: Array[String]): Unit = {def abs(x:Int) = x match {case i:Int if i >= 0 => icase i:Int if i <= 0 => -icase _ => "illegal"}println(abs(-5)) //5println(abs(5)) //5}}
模式匹配类型
匹配常量
object test {def main(args: Array[String]): Unit = {def describeConst(x:Any) = x match {case 1 => "Int one"case "hello" => "String hello"case true => "Boolean true"case '+' => "Char +"case _ => "illegal"}println(describeConst("hello")) //String helloprintln(describeConst('+')) //Char +println(describeConst(0.3)) //illegal}}
匹配类型
object test {def main(args: Array[String]): Unit = {def describeType(x:Any) = x match {case i:Int => "Int " + icase s:String => "String " + scase list:List[String] => "List " + listcase array: Array[Int] => "Array[Int] " + array.mkString(",")case a => "Something else:" + a}println(describeType(35)) //Int 35println(describeType("hello")) //String helloprintln(describeType(List("hello","scala"))) //List List(hello, scala)println(describeType(List(1,2,3))) //List List(1, 2, 3)println(describeType(Array("hello","scala"))) //Something else:[Ljava.lang.String;@75bd9247println(describeType(Array(1,2,3))) //Array[Int] 1,2,3}}
匹配数组
object test {def main(args: Array[String]): Unit = {for (arr <- List(Array(0),//0Array(1, 0),//Array(1,0)Array(0, 1, 0),//以0开头的数组Array(1, 1, 0),//中间为1的三元素数组Array(2, 3, 7, 15),//something elseArray("hello", 20, 30),//something else)) {val result = arr match {case Array(0) => "0"case Array(1, 0) => "Array(1,0)"case Array(x, y) => "Array: " + x + "," + ycase Array(0, _*) => "以0开头的数组"case Array(x, 1, z) => "中间为1的三元素数组"case _ => "something else"}println(result)}}}
匹配列表
object test {def main(args: Array[String]): Unit = {for (list <- List(List(0), //0List(1,0), //List(x,y):1,0List(0,0,0), //List(0,....)List(1,1,0), //List(0,....)List(88) //List(0,....))){val result = list match {case List(0) => "0"case List(x,y) => "List(x,y):" + x + ',' + ycase List(x,_*) => "List(0,....)"case List(a) => "List(a):" + acase _ => "something else"}println(result)}val list = List(1,5,7,9,24) //first:1,second:5,rest:List(7, 9, 24)val list1 = List(1) //something elselist match {case first :: second :: rest => println(s"first:$first,second:$second,rest:$rest")case _ => println("something else")}list1 match {case first :: second :: rest => println(s"first:$first,second:$second,rest:$rest")case _ => println("something else")}}}
匹配元组
object test {def main(args: Array[String]): Unit = {for (tuple <- List((0, 1), //0,1(0, 0), //0,0(0, 1, 0), //(a,1,_) 0(0, 1, 1), //(a,1,_) 0(1, 23, 56), //(x,y,z):12356("hello", true, 0.5), //(x,y,z):hellotrue0.5)) {val result = tuple match {case (a, b) => "" + a + "," + bcase (0, _) => "(0,_)"case (a, 1, _) => "(a,1,_) " + acase (x, y, z) => "(x,y,z):" + x + "" + y + "" + zcase _ => "something else"}println(result)}}}
