Scala深入浅出实战经典:27,Type、Array、List、Tuple模式匹配实战解析

package com.dt.scalaInAction.demo_027

/**
 * Type、Array、List、Tuple模式匹配实战解析
 */
object Pattern_Match_More {
    def main(args: Array[String]): Unit = {
        
        /**
         * 通过模式匹配判断参数的类型
         */
        def match_type(t: Any) = t match {
            case p: Int => println("It is Integer") //这里的p变量 val p: Int  t会赋值给变量p
            case p: String => println("It is String, the content is " + p)
            case m: Map[_, _] => m.foreach(println)  //val m: Map[_, _]
            case _ => println("Unknow type!!!")
        }
        match_type(2)                         //It is Integer
        match_type("Spark")                   //It is String, the content is Spark
        match_type(Map("Scala" -> "Spark"))   //(Scala,Spark)
        
        /**
         * 模式匹配数组
         */
        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 ...")     //匹配至少是1个元素,并且第一个元素为0的数组  _*表示后面有许多元素
            case _ => println("something else")
        }
        match_array(Array(0))                    //Array0
        match_array(Array(0, 1))                 //Array0 1
        match_array(Array(0, 1, 2, 3, 4, 5, 6))  //Array0 ...
        
        
        /**
         * 模式匹配List
         */
        def match_list(lst: Any) = lst match {
            case 0 :: Nil => println("List" + "0")                //匹配有一个元素的且为0的集合
            case x :: y :: Nil => println("List" + x + " " + y)   //匹配有两个元素的集合
            case 0 :: tail => println("List" + "0 ...")           //x :: tail 表示开头的元素为0 tail表示剩下的若干个元素  匹配至少有一个元素的集合
            case _ => println("something else")
        }
        match_list(List(0))                  //List0
        match_list(List(0, 1))               //List0 1
        match_list(List(0, 1, 2, 3, 4, 5))   //List0 ...
        
        /**
         * 模式匹配Tuple元祖
         */
         def match_tuple(tuple: Any) = tuple match {
            case (0, _) => println("Tuple" + "0")   //匹配第一个元素为0,第二个元素任意
            case (x, 0) => println("Tuple:" + x)    
            case _ => println("something else")
        }
        match_tuple((0, "Scala"))         //Tuple0
        match_tuple((2, 0))               //Tuple:2
        match_tuple((0, 1, 2, 3, 4, 5))   //something else

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值