1. 程式人生 > >Scala模式匹配 match case

Scala模式匹配 match case

1.匹配字串/型別/守衛

val arr = Array("YoshizawaAkiho", "YuiHatano", "AoiSola")

val i = Random.nextInt(arr.length)

println(i)

val name = arr(i)

println(name)

name match {

case "YoshizawaAkiho" => println("吉澤老師...")

case "YuiHatano" => {

println("波多老師...")

}

case _ => println("真不知道你們在說什麼...")

}

//定義一個數組

val arr:Array[Any] = Array("hello123", 1, 2.0, CaseDemo02, 2L)

//取出一個元素

val elem = arr(3)

elem match {

case x: Int => println("Int " + x)

case y: Double if(y >= 0) => println("Double "+ y) // if 守衛

case z: String => println("String " + z)

case w: Long => println("long " + w)

case CaseDemo02 => {

println("case demo 2")


}

case _ => { 
// 其他任意情況

println("no")

println("default")

}

}

2.匹配陣列

示例:

val arr = Array(1, 1, 7, 0, 2,3)

arr match {

case Array(0, 2, x, y) => println(x + " " + y)

case Array(2, 1, 7, y) => println("only 0 " + y)

case Array(1, 1, 7, _*) => println("0 ...") 
// _* 任意多個 
case _ => println("something else")

}

3.匹配集合

val lst = List(0, 3, 4)

println(lst.head)

println(lst.tail)

lst match {

case 0 :: Nil => println("only 0")

case x :: y :: Nil => println(s"x $x y $y")

case 0 :: a => println(s"value : $a")

case _ => println("something else")

}

4.匹配元組

val tup = (1, 3, 7)

tup match {

case (3, x, y) => println(s"hello 123 $x , $y")

case (z, x, y) => println(s"$z, $x , $y")

case (_, w, 5) => println(w)

case _ => println("else")

}

5.匹配樣例/樣例物件

//樣例類,模式匹配,封裝資料(多例),不用 new 即可建立例項 case class SubmitTask(id: String, name: String)

case class HeartBeat(time: Long)

//樣例物件,模式匹配(單例)

case object CheckTimeOutTask

val arr = Array(CheckTimeOutTask, new HeartBeat(123), HeartBeat(88888), new HeartBeat(666), SubmitTask("0001", "task-0001"))

val i = Random.nextInt(arr.length)

val element = arr(i)

println(element)

element match {

case SubmitTask(id, name) => {

println(s"$id, $name")

}

case HeartBeat(time) => {

println(time)

}

case CheckTimeOutTask => {

println("check")

}

}