标签:val lis 学习 log list array blog cal bsp
集合的模式匹配操作:
object CollectionDemo5 { def main(args: Array[String]): Unit = { //集合模式匹配1 val ls = List(1, 2, 3, 4, 5) println( ls match { case List(1, 2, 3) => "List(1,2,3)" case List(1, 2, 3, 4) => "List(1,2,3,4)" case List(1, 2, 3, 4, 5) => "List(1,2,3,4,5)" case other => "No match" }) //集合模式匹配2 val ls2 = List(9, 2, 3, 4, 5, 6) println( ls2 match { case List(1, 2, 3) => "List(1,2,3)" case List(1, 2, 3, 4) => "List(1,2,3,4)" case List(_, _, 3, 4, x) => "List(1,2,3,4,5)" + x case _ => "No match" }) //集合模式匹配3 val ls3 = List(9, 2, 3, 4, 5, 6) println( ls3 match { case x :: y :: z => z case Nil => "No match" }) //集合模式匹配4 val ls4 = (1, 2, 3, 4, 5, 6) println( ls4 match { case (_,_,x,_,_,_) => x case _ => "No match" }) } }
运行结果:
List(1,2,3,4,5)
No match
List(3, 4, 5, 6)
3
标签:val lis 学习 log list array blog cal bsp
原文地址:http://www.cnblogs.com/AK47Sonic/p/7078378.html