问题
偶然看到一段代码如下
public void test(String... strs) {
for (strs thisStr : strs) {
log.info("这个str是{}", thisStr);
}
}
内心想法
这是什么,代码还能打省略号的吗?
解决
其实!这是Java中的可变长参数!
以demo为例,test方法共支持三种调用方式哦~
test() | 无参数 |
test(“HelloWorld”) | 单个参数 |
test(new String[]{"Hello","World"}) | 多个参数 |