I wanted to write a function that would take an object and convert it to an array that contains that object as a single element. It occurred to me that I could maybe do this with generics and variable arguments to essentially do this automatically, without the need to write a function for each object type I wished to use. Will this code work? Are there any subtleties I need to be aware of?
public static <X> X[] convert_to_array(X... in_objs){ return in_objs; }
It works but it seems like:
is a little more straightforward then:
In cases where sometimes I want to pass a single object, but other times I want to pass an array, usually I just use an overloaded method in the API:
Varargs can be used but only if the array is the last parameter of course.