简体   繁体   English

创建链接列表的链接列表时出错

[英]Error while creating LinkedList of LInkedLists

I am trying to create a LinkedList of LinkedLists in Java.我正在尝试在 Java 中创建 LinkedList 的 LinkedList。

The following code segment is giving an error.以下代码段给出错误。 I am using java 11 and util.List我正在使用 java 11 和 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<>());

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

How should I go on resolving this?我应该如何 go 解决这个问题?


Okay, so just to test I created a dummy class just to create LinkedList of LinkedLists.好的,所以只是为了测试我创建了一个虚拟 class 只是为了创建 LinkedList 的 LinkedList。 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<>());    
    }
}

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

Edit: Okay, works fine when I use import java.util.List and import java.util.linkedList instead of import java.util.*编辑:好的,当我使用 import java.util.List 和 import java.util.linkedList 而不是 import java.util* 时工作正常。

As pointed out in the comments there is probably some issue with my build path正如评论中所指出的,我的构建路径可能存在一些问题

I've tried your example using java7 and using java8, and it gives me the same error as you are seeing for java7, but works for me for java8.我已经使用 java7 和 java8 尝试了你的示例,它给了我与你在 java7 中看到的相同的错误,但对我来说适用于 java8。

Why it doesn't work for java7 will have to do with limitations of type inference in that version of the compiler.为什么它不适用于 java7 将与该版本编译器中类型推断的限制有关。

I would expect java11 to work at least as well as java8 (which is to say, the code should compile using java11).我希望 java11 至少能像 java8 一样工作(也就是说,代码应该使用 java11 编译)。 Can you double check your compiler settings?你能仔细检查你的编译器设置吗? You may be using a java11 compiler, but may have set it to generate code using the java7 rules.您可能正在使用 java11 编译器,但可能已将其设置为使用 java7 规则生成代码。

Here is the version of the code that I tested:这是我测试的代码版本:

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<>() );
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM