让Java方法调用处理在程序执行时传入的可变参数

huangapple go评论77阅读模式
英文:

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));

huangapple
  • 本文由 发表于 2020年9月21日 00:18:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/63981110.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定