请考虑以下代码:
public class SimpleTest {
public static void print(Collection<Object> strings) {
System.out.println("Collection overload: " + strings);
}
public static void print(Object... strings) {
System.out.println("Vararg overload: " + Arrays.asList(strings));
}
@Test
public void test() {
List<String> strings = Arrays.asList("hello", "world");
print(strings);
}
}
您对输出的预测是什么?
事实证明,正确的答案是这个!
Vararg overload: [[hello, world]]
如果你猜对了,请你解释为什么不是收集过载?
更新
如果我将集合重载更改为
public static void print(Collection<String> strings) {
System.out.println("Collection overload: " + strings);
}
或
public static void print(Collection strings) {
System.out.println("Collection overload: " + strings);
}
然后输出
Collection overload: [hello, world]