How to make a variatic method take a single array as the first value of the varargs array?

arrays, java, variadic-functions

Solution

Typecast it while passing and you will get what you want -

m((Object)ab);

Problem

Given the variables: ``` Object[] ab = new Object[] { "a", "b" }; Object[] cd = new Object[] { "c", "d" }; ``` When calling the following method: ``` public static void m(Object... objects) { System.out.println(Arrays.asList(objects)); } ``` Using: ``` m(ab, cd); ``` I get the expected output: ``` [[Ljava.lang.Object;@3e25a5, [Ljava.lang.Object;@19821f] ``` But when using: ``` m(ab); ``` I get: ``` [a, b] ``` Since `strings <- ab` and not `strings[0] <- ab`. How can I force the compiler to take the `ab` array as the first value of the `strings` array, and then having the output: ``` [Ljava.lang.Object;@3e25a5 ``` ?

Original source