I've found it's available in Ruby, but I recognize it from what I've done in Python; the "splat" operator. Long story short, I'm wondering if there's a simpler way to accomplish what I currently am, mimicking what the "splat" operator does.
I made a central method that the rest can call because I realized I have several very similar ones, and they were all doing the same except for a few minor things. Here's the method signature:
private String callScript(String scriptLocation, String... extraArgs) throws Exception {
I want to require at least one argument (the scriptLocation
), and then allow any number of extra arguments. What I end up doing with this is creating a ProcessBuilder
. My desire is to do something like this:
ProcessBuilder pb = new ProcessBuilder("something", scriptLocation, /* extraArgs */);
But of course, the ProcessBuilder
constructor only accepts things like:
List<String>
String[]
String...
So my approach obviously wouldn't work.
My current workaround, which works fine as far as I know, is:
List<String> finalArgs = new ArrayList<String>();
finalArgs.add("something");
finalArgs.add(scriptLocation);
finalArgs.addAll(Arrays.asList(extraArgs));
ProcessBuilder pb = new ProcessBuilder(finalArgs);
(yes, I understand I don't have to use List
and could just make a String[]
)
(and yes, I understand I could loop through extraArgs
and add them to finalArgs
individually, instead of having to use addAll
and Arrays.asList()
)
(and yes, I know I can make a function that effectively accomplishes my workaround by returning certain arguments combined with variable arguments)
So I guess outside of these last three statements, is there anything that can achieve this?
String...
is actuallyString[]
. If you split your parameters likeString s1
,String[] s2ToN
to enforce the first parameter, creating a copy in a new array / list is the only thing you can do to merge them. – Youngsarray[]
is final in it's size. If you need to add one element you have to create a bigger array and copy data in there.ArrayList
does the same internally. There are ways to make that operation more efficient than you did but that's it. (e.g.System.arraycopy()
) – YoungsSystem.arraycopy()
to my example), I'd happily accept it over the other answer (no offense to them). – Choline