怎么使用scala模式匹配空数组
使用scala模式匹配空数组的方法如下:
val arr = Array[Int]() // 创建一个空数组
arr match {
case Array() => println("空数组") // 匹配空数组
case _ => println("非空数组")
}
在上面的例子中,我们创建了一个空数组arr
。然后使用模式匹配,case Array()
匹配空数组,case _
匹配非空数组。根据匹配结果打印不同的输出。
输出结果为:空数组
相关问答