java:我怎样才能创建一个支持任意数量参数的函数? [英] java: how can i create a function that supports any number of parameters?
本文介绍了java:我怎样才能创建一个支持任意数量参数的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以在java中创建一个支持任意数量参数的函数,然后能够遍历提供给函数的每个参数?
is it possible to create a function in java that supports any number of parameters and then to be able to iterate through each of the parameter provided to the function ?
谢谢
kfir
推荐答案
喜欢这个......
Like this...
public void func(String ... strings) {
for (String s : strings)
System.out.println(s);
}
注意,如果你想要要求一些最小数量的参数必须是传递给函数,同时仍然允许变量参数,你应该做这样的事情。例如,如果你有一个需要至少一个字符串的函数,然后是一个可变长度参数列表:
Note that if you wanted to require that some minimal number of arguments has to be passed to a function, while still allowing for variable arguments, you should do something like this. For example, if you had a function that needed at least one string, and then a variable length argument list:
public void func2(String s1, String ... strings) {
}
这篇关于java:我怎样才能创建一个支持任意数量参数的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文