一个模式匹配语句包括一个待匹配的值,match关键字,以及至少一个case语句。 match 对应 Java 里的 switch,但是写在选择器表达式之后。即: 选择器 match {备选项}。 match 表达式通过以代码编写的先后次序尝试每个模式来完成计算,只要发现有一个匹配的case,剩下的case不会继续匹配。
1 2 3 4 5 6 7 8 9 10
import scala.util.Random
val x: Int = Random.nextInt(10)
x match { case0 => "zero" case1 => "one" case2 => "two" case _ => "other" }
defwhat(animal: Animal): String = animal match { caseCat(name: String, color: String) => s"cat's is $name,color $color" caseDog(name: String, color: String, age: Int) => s"dog's is $name,color $color ,age $age" } val a1 = Cat("多啦A梦", "白色") val s1 = what(a1) println(s1) //a cat name is 多啦A梦,color is 白色
val a2 = Dog("多啦B梦", "白色", 500) val s2 = what(a2) println(s2) //a dog name is 多啦B梦,color is 白色 ,age is 500
集合类型匹配
数组匹配
1 2 3 4 5 6 7
defm5(arr: Array[Int]) = arr match { caseArray(1, x, y) => println("匹配以1 开头,有三个元素的数组"); caseArray(0) => println("匹配只有 0 这个元素的数组"); caseArray(0, _*) => println("匹配以0 开头任意多个元素的数组"); case arr if arr.length == 2 => println("length = 2") case _ => println("unknow") }
序列匹配
1 2 3 4 5 6 7 8 9 10 11
defm5_1(list: List[Int]) = list match { case5 :: Nil => println("匹配只有 5 这个元素的序列") case x :: y :: Nil => println("匹配只有两个元素的序列") case list if list.last == 1 => println("结尾是1的列表") case x :: tail => println("匹配任意多个元素的数组") case _ => println("unknow") }
defm6(tuple: Any) = tuple match { case (x, y, 7) => println("匹配有三个元素并且以7 结尾的元组"); case (2, x, y) => println("匹配以2 开头有三个元素的元组"); case _ => println("unknow") }
defm7_1(v: Any) = v match { casenull => "null" case i: Int => i * 100 case s: String => s case _ => "others" } // 注意:上面case中的i、s都叫模式变量 println(m7_1(null)) // "null" println(m7_1(5)) // 500 println(m7_1("hello")) // "hello" println(m7_1(3.14)) // "others"
deftraverse(t: BinaryTree): Unit = { t match { casenull => Unit caseNode(v, left, right) => traverse(left);println(v);traverse(right) // 中序 //case Node(v, left, right) => println(v); traverse(left); traverse(right) //前序 //case Node(v, left, right) => traverse(left);traverse(right);println(v) //后序 } }
defBFS(t: (List[Node], List[Int])): (List[Node], List[Int]) = t match { case (Nil, res) => (Nil, res) case (lB, res) => { val tmp = lB.map { caseNode(v, null, null) => (Nil, v) caseNode(v, left, null) => (List(left.asInstanceOf[Node]), v) caseNode(v, null, right) => (List(right.asInstanceOf[Node]), v) caseNode(v, left, right) => (List(left.asInstanceOf[Node], right.asInstanceOf[Node]), v) } BFS((tmp.flatMap(_._1), res ++ tmp.map(_._2))) } }
defDFS(t:(List[Node], List[Int])): (List[Node], List[Int]) =t match{ case (Nil, res) => (Nil, res) case (lB, res) => { val top = lB.head val left = if(top.left == null) NilelseList(top.left.asInstanceOf[Node]) val right = if(top.right == null) NilelseList(top.right.asInstanceOf[Node]) DFS(left ++ right ++ lB.tail, res ++ List(top.value)) } }
defmain(args: Array[String]): Unit = { val t = Node(1, Node(2, Node(4), Node(5, Node(7), Node(8))), Node(3, null, Node(6))) //traverse(t) val res = DFS((List(t), List.empty[Int])) println(res._2) // List(1, 2, 4, 5, 7, 8, 3, 6)