可变长度参数必须是参数列表中的最后一个,不然会报错String ...paramMore与String...paramMore、String... paramMore等效
import org.apache.commons.lang3.StringUtils; public class TestMain { public static void main(String[] args) { prams("zhangsan", "lisi", "wangwu"); } public static void prams(String param1, String ...paramMore) { System.out.println("param1 is : " + param1); System.out.println("paramMore is : " + StringUtils.join(paramMore, ",")); } }
执行结果:
param1 is : zhangsan paramMore is : lisi,wangwu示例2
可变长度参数其实是个数组可变长度参数可不填写,数组长度为0(不是null)
import org.apache.commons.lang3.StringUtils; public class TestMain { public static void main(String[] args) { prams("zhangsan"); } public static void prams(String param1, String ...paramMore) { System.out.println("param1 is : " + param1); System.out.println("paramMore length : " + paramMore.length); System.out.println("paramMore is : " + StringUtils.join(paramMore, ",")); } }
执行结果:
param1 is : zhangsan paramMore length : 0 paramMore is :
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)