Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java中的泛型类型对象类型不匹配_Java_Generics_Linked List_Declaration_Type Mismatch - Fatal编程技术网

java中的泛型类型对象类型不匹配

java中的泛型类型对象类型不匹配,java,generics,linked-list,declaration,type-mismatch,Java,Generics,Linked List,Declaration,Type Mismatch,我试图创建一个LinkedList类。但我有两个问题: 1Node last=null声明给了我一个原始类型错误,但在该声明的上面没有类似的错误。4个相同的声明,但只有最后一个给出错误 2在get方法中,我想返回V type,正如您所看到的,value变量已经在V type中了。但它给了我无法将对象转换为V的错误。但临时值已经是V public class Linkedlist<V> { public class Node <V> { priv

我试图创建一个LinkedList类。但我有两个问题:

1Node last=null声明给了我一个原始类型错误,但在该声明的上面没有类似的错误。4个相同的声明,但只有最后一个给出错误

2在get方法中,我想返回V type,正如您所看到的,value变量已经在V type中了。但它给了我无法将对象转换为V的错误。但临时值已经是V

public class Linkedlist<V> {

    public class Node <V> {

        private Node next=null;

        private String key;

        private int size;

        private V value=null;

        public Node(V value, String key){
            this.key=key;
            this.value=value;
        }
    }

    Node root=null;
    Node temp=null;
    Node temp1=null;
    Node last=null;

    last=root;

    public void add(V value, String key){
        last.next = new Node(value,key);
        last=last.next;
    }


    public void remove(String key){
        temp=root;
        if(isEmpty())
        System.out.println("list is empty!");

        else{
            if(temp.next!=null){

                if(!temp.next.key.equals(key)){
                    remove(temp.next.key);
                }

                else if(temp.next.key.equals(key)){
                    if(temp.next==last)
                    last=temp;
                    temp.next=temp.next.next;
                }
            }
            else
            System.out.println("there is no such element");
        }
    }


    public V get(String key){
        temp=root;

        if(temp.key.equals(key)){
            if(temp.next!=null)
            get(temp.next.key);

            else
            return null;
        }
        else if(temp.key.equals(key))
        return temp.value;

    }

你提到的两个问题实际上是同一个问题。给定您声明的参数化类节点,这些

Node root=null;
Node temp=null;
Node temp1=null;
Node last=null;
。。。原始类型节点的所有声明对象。其他代码将解释它们,就像它们的类型参数被指定为对象一样。您应该这样声明:

Node<V> root=null;
Node<V> temp=null;
Node<V> temp1=null;
Node<V> last=null;
其中是文本-对类的类型参数的引用,而不是对任何具体类型的引用。如果这样做,那么两个错误都会消失。

您的内部类节点是泛型的,但您使用的是类的原始形式。这意味着一个返回V的方法会被擦除类型,现在它返回的是Object。但是Node类不需要是泛型的。非静态嵌套类,即嵌套类可以使用其封闭类的泛型类型参数。因此,请删除节点类定义上的

public class Node {
我看到的其他问题:

last=根;似乎在任何构造函数、方法或初始化块之外。 我没有看到一个isEmpty方法,但为了简洁起见,您可能没有发布它。 如果既不满足if条件,也不满足else条件,则get方法需要一个return语句。
你能修改一下代码的缩进吗。这很难读。我是一个编码新手。对不起,在你阅读的时候让你痛苦的一切。也很抱歉我的英语很糟糕。更不用说get方法中的else if块的条件与其初始if完全相同,因此从Node类中删除无法访问的代码有帮助,谢谢。然而,我试图在构造函数中声明变量,但其他方法没有看到变量。我必须让它们全球化。我该怎么做?此外,isEmpty方法存在,但我不想将其复制到此处。构造函数中声明的变量是局部变量,仅在构造函数体的作用域中。你可以让它们成为实例变量,就像你对root、temp、temp1和last所做的那样。当我按照你说的做的时候,它给了我一个我在问题中提到的原始类型错误。如果不知道你在制造一个实例变量是什么局部变量,很难说。我在你的节点构造函数中没有看到任何局部变量。我试过了,但没有改变任何东西。我又犯了一个错误。此外,我还根据Targetman的建议删除了Node类,这有助于消除convert object to V错误。