我想做这样的事情:
ArrayList<String> strings = new ArrayList<>();
//build a collection of Strings
callMethod(strings);
Run Code Online (Sandbox Code Playgroud)
“callMethod”是遗留的(在遗留项目中),所以我无法更改它。
它的签名是这样的:
private void callMethod(String... input){
// and so forth...
Run Code Online (Sandbox Code Playgroud)
如何将字符串集合传递给 callMethod 方法?
只需将列表转换为字符串数组(varargs 在底层转换为数组:“如果形式参数是可变参数,则声明的类型是由 \xc2\xa710.2 指定的数组类型。”):
\ncallMethod(strings.toArray(new String[0]));\nRun Code Online (Sandbox Code Playgroud)\n