Error while creating LinkedList of LInkedLists

Lit*_*iot 5 java collections

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 arguments for LinkedList
            for( i = 0;i<N;i++) L.add(new LinkedList<>());
                                                    ^
  reason: cannot use '<>' with non-generic class LinkedList
Run Code Online (Sandbox Code Playgroud)

How should I go on resolving this?


Okay, so just to test I created a dummy class just to create LinkedList of LinkedLists. Here is the full program:

import java.util.*;
class Dummy
{
    public static void main(String[] args) 
    {
        List<List<Integer>> L;
        L = new LinkedList<>();
        for(int i = 0;i<10;i++) L.add(new LinkedList<>());    
    }
}
Run Code Online (Sandbox Code Playgroud)

Again, these errors:

A.java:7: error: cannot infer type arguments for LinkedList
        L = new LinkedList<>();
                          ^
  reason: cannot use '<>' with non-generic class LinkedList
A.java:8: error: cannot infer type arguments for LinkedList
        for(int i = 0;i<10;i++) L.add(new LinkedList<>());    
                                                    ^
  reason: cannot use '<>' with non-generic class LinkedList

Run Code Online (Sandbox Code Playgroud)

Edit: Okay, works fine when I use import java.util.List and import java.util.linkedList instead of import java.util.*

As pointed out in the comments there is probably some issue with my build path

Tho*_*nti 1

我已经尝试过使用 java7 和 java8 的示例,它给出了与 java7 相同的错误,但适用于 java8。

为什么它不适用于 java7 与该版本编译器中类型推断的限制有关。

我希望java11至少能像java8一样工作(也就是说,代码应该使用java11编译)。你能仔细检查你的编译器设置吗?您可能使用的是 java11 编译器,但可能已将其设置为使用 java7 规则生成代码。

这是我测试的代码版本:

import java.util.List;
import java.util.LinkedList;

public class TypeTest {
    private static final int STORAGE_SIZE = 10;

    private static final List<List<Integer>> storage = new LinkedList<>();

    static {
        for ( int elementNo = 0; elementNo < STORAGE_SIZE; elementNo++ ) {
            storage.add( new LinkedList<>() );
        }
    }
}
Run Code Online (Sandbox Code Playgroud)