基本语法
val num1 = 10
val num2 = 20
val option = '&'
val res: Any = option match {
case '+' => num1 + num2
case '-' => num1 - num2
case '*' => num1 * num2
case '/' => num1 / num2
case _ => "运算符不合法"
}
println( res)
说明
如果所有case都不匹配,那么会执行case _ 分支,类似于Java中default语句,若此时没有case _ 分支,那么会抛出MatchError。 每个case中,不需要使用break语句,自动中断case。 match case语句可以匹配任何类型,而不只是字面量。 => 后面的代码块,直到下一个case语句之前的代码是作为一个整体执行,可以使用{}括起来,也可以不括。
模式守卫
val num3 = - 10
def myAbs( num: Int ) = num3 match {
case i: Int if i >= 0 => i
case j: Int if j < 0 => - j
}
println( myAbs( num3) )
匹配常量
def main( args: Array[ String ] ) : Unit = {
println( describe( 6 ) )
}
def describe( x: Any ) = x match {
case 5 => "Int five"
case "hello" => "String hello"
case true => "Boolean true"
case '+' => "Char +"
}
匹配类型
def describe( x: Any ) = x match {
case i: Int => "Int"
case s: String => "String hello"
case m: List[ _] => "List"
case c: Array[ Int ] => "Array[Int]"
case someThing => "something else " + someThing
}
def main( args: Array[ String ] ) : Unit = {
println( describe( List( 1 , 2 , 3 , 4 , 5 ) ) )
println( describe( Array( 1 , 2 , 3 , 4 , 5 , 6 ) ) )
println( describe( Array( "abc" ) ) )
}
匹配数组
def main( args: Array[ String ] ) : Unit = {
for ( arr <- Array( Array( 0 ) , Array( 1 , 0 ) , Array( 0 , 1 , 0 ) , Array( 1 , 1 , 0 ) , Array( 1 , 1 , 0 , 1 ) , Array( "hello" , 90 ) ) ) {
val result = arr match {
case Array( 0 ) => "0"
case Array( x, y) => x + "," + y
case Array( 0 , _* ) => "以0开头的数组"
case _ => "something else"
}
println( "result = " + result)
}
}
匹配列表
for ( list <- Array( List( 0 ) , List( 1 , 0 ) , List( 0 , 0 , 0 ) , List( 1 , 0 , 0 ) , List( 88 ) ) ) {
val result = list match {
case List( 0 ) => "0"
case List( x, y) => x + "," + y
case List( 0 , _* ) => "0 ..."
case _ => "something else"
}
println( result)
}
val list: List[ Int ] = List( 1 , 2 , 5 , 6 , 7 )
list match {
case first : : second : : rest => println( first + "-" + second + "-" + rest)
case _ => println( "something else" )
}
匹配元组
for ( tuple <- Array( ( 0 , 1 ) , ( 1 , 0 ) , ( 1 , 1 ) , ( 1 , 0 , 2 ) ) ) {
val result = tuple match {
case ( 0 , _) => "0 ..."
case ( y, 0 ) => "" + y + "0"
case ( a, b) => "" + a + " " + b
case _ => "something else"
}
println( result)
}
元组中的特殊匹配
输出元组中的第一个元素
val list: List[ ( String , Int ) ] = List( ( "a" , 1 ) , ( "b" , 2 ) , ( "c" , 3 ) )
for ( elem <- list) {
println( elem. _1)
}
for ( ( word, count) <- list) {
println( word)
}
for ( ( word, _) <- list) {
println( word)
}
某个元素对应的值
for ( ( "a" , count) <- list) {
println( count)
}
在元素匹配时,为元组元素命名
val ( name, age, addr) : ( String , Int , String ) = ( "zhangsan" , 18 , "北京" )
println( name)
key不变,value*2
val list: List[ ( String , Int ) ] = List( ( "a" , 1 ) , ( "b" , 2 ) , ( "c" , 3 ) )
for ( ( word, count) <- list) {
println( ( word, count * 2 ) )
}
println( list. map( kv => { ( kv. _1, kv. _2* 2 ) } ) )
val tuples: List[ ( String , Int ) ] = list. map { t =>
t match {
case ( word, count) => ( word, count * 2 )
}
}
println( tuples)
val tuples: List[ ( String , Int ) ] = list. map {
case ( word, count) => ( word, count * 2 )
}
println( tuples)
val list = List( ( "a" , ( "a" , 5 ) ) , ( "b" , ( "b" , 10 ) ) , ( "c" , ( "c" , 20 ) ) )
val tuples: List[ ( String , ( String , Int ) ) ] = list. map {
case ( word, ( word2, count) ) => ( word, ( word2, count* 2 ) )
}
println( tuples)
匹配对象
模式匹配对象时,会调用伴生对象中的unapply方法获取属性的值。
object Scala04_Match {
def main( args: Array[ String ] ) : Unit = {
val stu = Student04( "zhangsan" , 21 )
val res = stu match {
case Student04( "zhangsan" , 22 ) => true
case _ => false
}
println( res)
}
}
class Student04( var name: String , var age: Int ) { }
object Student04{
def apply( name: String , age: Int ) : Student04 = new Student04( name, age)
def unapply( stu: Student04) : Option[ ( String , Int ) ] = {
if ( stu == null ) {
None
} else {
Some( stu. name, stu. age)
}
}
}
样例类
声明类时用case修饰。 样例类底层会自动实现unapply方法,获取对象的属性。
object Scala05_Match {
def main( args: Array[ String ] ) : Unit = {
val stu = new Student05( "lisi" , 18 )
val res = stu match {
case Student05( "lisi" , 18 ) => true
case _ => false
}
println( res)
}
}
case class Student05( var name: String , var age: Int ) { }