英文:
Have Java method call deal with varargs passed in on program execution
问题
我有这个Java方法调用:
csvOperations.readCsv(csvFile, templateId, args[2], args[3], args[4]);
这是一个示例调用。在程序执行时可以传递任意数量的args参数,我希望我的方法调用能够适应任何长度的参数。应该如何实现?
我的方法声明如下:
public void readCsv(String csvFile, String templateId, String ... keyId)
英文:
I've this Java method call:
csvOperations.readCsv(csvFile, templateId, args[2], args[3], args[4]);
This is an example call. Any number of args could be passed in on program executions and I'd like my method call to be able to cater for whatever length that is. How is this done?
My method declaration is like this:
public void readCsv(String csvFile, String templateId, String ... keyId)
答案1
得分: 2
最简单的方法可能是使用 Arrays.copyOfRange
将部分数组传递给您的方法:
csvOperations.readCsv(csvFile, templateId, Arrays.copyOfRange(args, 2, arg.length));
英文:
The easiest approach would probably be to use Arrays.copyOfRange
to pass a partial array to your method:
csvOperations.readCsv(csvFile, templateId, Arrays.copyOfRange(args, 2, arg.length));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论