模式匹配类型
1 object Test2_MatchTypes { 2 def main(args: Array[String]): Unit = { 3 //1、匹配常量 4 def describeConst(x: Any): String = x match { 5 case 1 => "Num one" 6 case "hello" => "String" 7 case true => "Boolean" 8 //这个下划线是一个占位符,不是通配符 9 case _ => "exit" 10 } 11 println(describeConst("hello")) 12 println(describeConst(true)) 13 println(describeConst(111)) 14 println("-----------") 15 16 //2、匹配类型 17 def describeType(x: Any) = x match { 18 case i: Int => "Int" + i 19 case s: String => "String" + s 20 case list: List[String] => "List: " + list 21 case array: Array[Int] => "Array: " + array.mkString(",") 22 case a => "exit" + a 23 } 24 25 println(describeType(2)) 26 println(describeType("scala")) 27 println(describeType(List("hi", "hello"))) 28 //List存在泛型擦除,泛型就是 List[String] 中的String 29 //所以能输出 List: List(1, 2), Int类型的List 30 println(describeType(List(1, 2))) 31 println(describeType(Array(1, 3, 5))) 32 println(describeType(Array("hi", "scala"))) 33 println("------------------") 34 35 //3、匹配数组 36 for (arr <- List( 37 Array(0), 38 Array(0, 2, 3), 39 Array(11, 33), 40 Array("hello", 2, 12) 41 )) { 42 val result = arr match { 43 //case匹配之后会取走数据,所以后面不会再匹配取走的数据 44 case Array(0) => "0" 45 case Array(0, 2, 3) => "Array(0, 2, 3)" 46 //匹配两个元素的数组 47 case Array(x, y) => x + ", " + y 48 case Array(0, _*) => "0开头的数组" 49 case Array(x, 2, y) => "匹配中间为2的数组" 50 case _ => "exit" 51 } 52 println(result) 53 } 54 println("-------------------") 55 56 //4、匹配列表 57 //5、方式一 58 for ( list <- List( 59 List(0), 60 List(1, 0), 61 List(0, 0, 0), 62 List(1, 1, 0), 63 List(33), 64 List("hello") 65 )) { 66 val result = list match { 67 case List(0) => "0" 68 case List(x, y) => x + ", " + y 69 case List(0, _*) => "0, ........" 70 case List(a) => a 71 case _ => "exit" 72 } 73 println(result) 74 } 75 //方式二 76 val list = List(1, 2, 4, 6, 7, 8) 77 val list1 = List(1) 78 list match { 79 case first :: second :: rest => println(s"first: $first, second : $second, rest: $rest") 80 case _ => println("exit") 81 } 82 83 list1 match { 84 case first :: second :: rest => println(s"first: $first, second : $second, rest: $rest") 85 case _ => println("exit!") 86 } 87 println("------------------------") 88 89 //5、匹配元组 90 for (tuple <- List( 91 (0, 1), 92 (0, 0), 93 (0, 1, 1), 94 (1, 1, 1), 95 (1, 2, 3), 96 ("hello", true, 0.3), 97 )) { 98 val result = tuple match { 99 case (a, b) => " " + a + ", " + b 100 case (0, _) => "第一个是0的二元组" 101 case (a, 1, _) => "中间是1的, " + a 102 case _ => "exit" 103 } 104 println(result) 105 } 106 } 107 }
元组匹配还有其他几种匹配方式:
1 object Test3_MatchTupleExtend { 2 def main(args: Array[String]): Unit = { 3 //1、在变量声明时,匹配 4 val (x, y) = (1, true) 5 println((x, y)) 6 7 //不用 :: 不能使用rest去赋值,只能用_* 8 val List(first, second, _*) = List(1, 2, 3, 4, 5, 1) 9 println((first, second)) 10 11 val fir :: sec :: rest = List(11, 22, 33 ,44 ,55) 12 println((fir, sec, rest)) 13 println("------------------------") 14 15 //2、for推导式中进行模式匹配 16 val list = List(("a", 1), ("b", 2), ("c", 3), ("a", 5)) 17 18 //2.1原本的便利方式 19 for (elem <- list) { 20 println(elem._1 + ", " + elem._2) 21 } 22 println("----------------") 23 24 //2.2 将List元素直接定义为元组,对变量赋值 25 for ((word, count) <-list) { 26 println(word + " ," + count) 27 } 28 println("--------------------") 29 30 //2.3 可以不考虑某个位置的变量,之遍历key或者value 31 for ((word, _) <- list) println(word) 32 println("---------------------") 33 34 //2.4 指定某个位置的值必须是多少 35 for (("a", count) <- list) println(count) 36 } 37 38 }