给出了泛型教程中的这个例子.
List<String> list = new ArrayList<>();
list.add("A");
// The following statement should fail since addAll expects
// Collection<? extends String>
list.addAll(new ArrayList<>());
Run Code Online (Sandbox Code Playgroud)
为什么最后一行不能编译,它似乎应该编译.第一行使用非常相似的构造并编译没有问题.
请详细解释.
I am trying to create a LinkedList of LinkedLists in Java.
The following code segment is giving an error. I am using java 11 and util.List
No idea why I am getting this error..
N = in.read();
List<List<Integer>> L;
L = new LinkedList<>();
for( i = 0;i<N;i++) L.add(new LinkedList<>());
Run Code Online (Sandbox Code Playgroud)
It gives the following errors:
A.java:25: error: cannot infer type arguments for LinkedList
L = new LinkedList<>();
^
reason: cannot use '<>' with non-generic class LinkedList
A.java:26: error: cannot infer type …Run Code Online (Sandbox Code Playgroud)