If I have a method public void foo(Object... x), I can call it in this way:
Object[] bar = ...;
foo(bar);
However, this doesn’t work:
Object baz = ...;
Object[] bar = ...;
foo(baz, bar);
Obviously, it can be done by creating an array with size 1 greater than bar and copying baz and the contents of bar there. But is there some more readable shortcut?
Guava’s
ObjectArraysclass provides methods to concatenate a single object to the beginning or end of an array, largely for this purpose. There’s no way to get around the linear overhead, but it’s already built and tested for you.