Scala allows the use of variable parameter lists.
grammar
Add an asterisk (*) after the declared parameter type
Example
object HelloWorld{ def hello(args:String*): Unit ={ for(arg <- args) println("hello "+arg) } def main(args:Array[String]): Unit ={ hello("zhangyuhang","zhangqiuyue") } }
result
hello zhangyuhang hello zhangqiuyue
We can see that when we define the args parameter, we indicate the parameter type, and add an asterisk after the parameter type to implement variable parameters.
However, we cannot use variadic parameters of different types, which means that there must be one type.
In fact, the parameters passed in by String* will be formed into an Array[String]
However, when we use it, we must pass in one parameter and one parameter, and the number is not limited.
Since it is not that the parameter is ultimately an Arrary[String] type, can I pass in an Array[String] type parameter?
No, it will report an error.
We use variable parameters in java to use ... Note the distinction.
public void hello(String... args){ for(String arg:args) System.out.println("hello "+arg); }