Scala学习笔记19【Scala模式匹配入门实战】

1、Scala模式匹配入门

package com.yl.Scala

object ScalaTest {
  def main(args: Array[String]){
    val number = 3
    number match{                            //match关键字
      case 1 => println("It is 1.")
      case 2 => println("It is 2.")
      case _ => println("It is not 1 or 2.") //“_” 表示若为其他值

      //灵活写法
     val result = number match{
        case i if i == 1  => "It is 1."
        case data if data == 2 => "It is 2."
        case _ => "It is not 1 or 2."
      }
      println("result = " + result)

      //字符串匹配案例
      "Sc al a" foreach{ c => println(
          c match{
            case space if space == ' ' => "space" //若为空格,结果为space
            case ch => "Char: " + ch              //若为字符,结果为“Char: 字符”
          })

      }
    }
  }
}

运行结果:

It is not 1 or 2.
result = It is not 1 or 2.
Char: S
Char: c
space
Char: a
Char: l
space
Char: a

2、Scala类型判断模式匹配实战

    //定义类型判断函数
    def  match_type(t : Any) = t match{
    case a: Int => println("这是个整型数据.")
    case a: String => println("这是个字符串类型数据.")
    case _  => println("未知类型。") 
    }

    match_type(2)     //这是个整型数据.
    match_type("aaa") //这是个字符串类型数据.

3、Scala Array模式匹配实战

    //定义匹配Array的函数
    def match_array(arr: Any) = arr match{
      case Array(0) => println("Array" + "0")
      case Array(x, y) => println("Array" + " " + x + " " + y)
      case Array(0, _*) => println("Array" + "0 ...")
      case _ => println("It's something else.")
    }

    match_array("sad")                   //It's something else.
    match_array(Array(0))                //Array0
    match_array(Array(3, 5))             //Array 3 5
    match_array(Array(0, 1, 2, 3, 4, 5)) //Array0 ...

4、Scala List模式匹配实战

    //定义匹配List的函数
    def match_list(lst: Any) = lst match{
      case Nil =>println("Empty list.")  
      case 0 :: Nil => println("List: " + "0")                 //含有一个0元素的List
      case 7 :: Nil => println("List: " + "7")                 //含有一个7元素的List
      case x :: y :: Nil => println("List: " + x + " " + y)    //含有x, y的List
      case 0 :: tail => println("List: " + "0 ...")            //可以有很多个元素的List
      case _ => println("It's something else.")
    }

    match_list(List())               //Empty list.
    match_list(List(0))              //List: 0
    match_list(List(7))              //List: 7
    match_list(List(0, 2))           //List: 0 2
    match_list(List(0,1,2,3,4,5,6))  //List: 0 ...

5、Scala Tuple模式匹配实战

    //定义tuple匹配的函数
    def match_tuple(tuple: Any) = tuple match{
      case(0, _) => println("Tuple: " + "0")
      case(x, 0) => println("Tuple: " + x)
      case _ => println("Something else.")
    }

    match_tuple((0, "Scala"))                //Tuple: 0
    match_tuple((24, 0))                     //Tuple: 24
    match_tuple((43, "Scala", 465, 546, 6))  //Something else.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值