相关疑难解决方法(0)

为什么菱形运算符不能在Java 7中的addAll()调用中工作?

给出了泛型教程中的这个例子.

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)

为什么最后一行不能编译,它似乎应该编译.第一行使用非常相似的构造并编译没有问题.

请详细解释.

java generics diamond-operator java-7

11
推荐指数
2
解决办法
1万
查看次数

Error while creating LinkedList of LInkedLists

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)

java collections

5
推荐指数
1
解决办法
67
查看次数

标签 统计

java ×2

collections ×1

diamond-operator ×1

generics ×1

java-7 ×1