如果我想做这样的事情:
List<?> unknownList = new ArrayList<>();
然后代码编译并运行良好,但是创建了哪种类型ArrayList
?
在这一行之后,如果我这样做了:
unknownList.add("str"); //compilation error
它给出了编译错误:
error: no suitable method found for add(String)
unList.add("str");
^
method List.add(int,CAP#1) is not applicable
(actual and formal argument lists differ in length)
method List.add(CAP#1) is not applicable
(actual argument String cannot be converted to CAP#1 by method invocation conversion)
method Collection.add(CAP#1) is not applicable
(actual argument String cannot be converted to CAP#1 by method invocation conversion)
where CAP#1 is a fresh type-variable:
CAP#1 extends Object from capture of ?
这是什么错误,将菱形运算符与通配符一起使用是否很好?如果是,那么在哪里???