Search code examples
javavariadic-functionsargs

Have Java method call deal with varargs passed in on program execution


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)

Solution

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