I would like to know if it is possible to unpack an Object array into separate Object on method call which accepts vargs. This question is similar to this one.
I have a
Well, there is no syntactic sugar à la Python or Lua for that in Java, but you can create your own unpack
method:
@SafeVarargs
public static Object[] unpack(E... objects) {
List
This will returns an array containing all the input elements, unpacked.
Then you can call it in your main
method:
res = doWork(unpack("three", res));
The reason I make this method generic is that you can call it with, for example, a String[]
array, without generating a warning. For some reason, the Java compiler thinks that this method has a chance of "polluting the heap", but it is not the case, that's why I added a @SafeVarargs
annotation.