Java – how to make the variable method take a single array as the first value of the varargs array?

Given the variable:

Object[] ab = new Object[] { "a","b" };
Object[] cd = new Object[] { "c","d" };

When the following methods are called:

public static void m(Object... objects) {
    System.out.println(Arrays.asList(objects));
}

use:

m(ab,cd);

I got the expected output:

[[Ljava.lang.Object;@3e25a5,[Ljava.lang.Object;@19821f]

However, when used:

m(ab);

Oh, I see:

[a,b]

Since the string < - AB is not the string [0] < - ab. how to force the compiler to take the AB array as the first value of the strings array and then output:

[Ljava.lang.Object;@3e25a5

Solution

You can get what you want when you pass by –

m((Object)ab);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>