public class VarargsParamVsLocalVariable {
static void f(List<String>... stringLists) {
// compiles fine! No problems in Runtime as well.
Object[] array = stringLists;
}
//but the same fails if List<String> is not a vararg parameter
public static void main(String[] args) {
List<String> stringLists;
List<String> stringLists1 = new ArrayList<>();
//below lines give: "cannot convert from List<String> to Object[]"
Object[] array = stringLists; // compile error!
Object[] array1 = stringLists1; // compile error!
}
}
// Why I can assign List<String> variable to Object[] variable if List<String> variable is a vararg parameter?
如果List变量是可变参数参数,为什么我可以将List变量赋值给Object []变量?为什么要编译“List <String> lst; Object [] o = lst;”如果列表<String>是可变参数?
这是因为'stringLists'是类型的'列表 []'的可变参数。 –
marstran
http://stackoverflow.com/questions/35455230/an-array-of-strings-vs-string-varargs的副本? –