How to pass a list as parameter for varargs? [duplicate]

你。 提交于 2021-02-08 06:38:34

问题


List<String> strings;
void takeParams(String... params);

How can I pass the list into that a method only taking varargs?

strings.toArray(); is not possible as it returns Object[].


回答1:


Use the List#toArray(T[]) method to create an array from the list, and pass it.

takeParams(strings.toArray(new String[strings.size()]));


来源:https://stackoverflow.com/questions/19951470/how-to-pass-a-list-as-parameter-for-varargs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!