scala 可变长度参数

 1 object Main {
 2   def main(args: Array[String]): Unit = {
 3     tellme("hello", "world")
 4     val arrMsgs = Array("I can", "do")
 5     tellme(arrMsgs: _*)
 6   }
 7 
 8   def tellme(msgs: String*): Unit ={
 9     msgs.foreach(println)
10   }
11 };

 

定义 tellme 函数,可以接受不定数量参数。参数 arrMsgs 是一个数组,是不可以直接作为 tellme 函数实参的。这里使用 arrMsgs: _* 告诉编译器把数组中每个元素作为参数,传入。

posted on 2021-05-26 23:22  一直小飞猫  阅读(90)  评论(0编辑  收藏  举报

导航