简体   繁体   English

Java中的单链表 - get()方法

[英]Singly linked list in Java - get() method

I am new to Java and trying to implement a singly linked list in Java. 我是Java的新手,并尝试在Java中实现单链表。 I have included the use of generics. 我已经包含了泛型的使用。 The code is shown below: 代码如下所示:

public class LinkedListX<E>{

   Node <E> head;
   int size;

   LinkedListX(){
       head = null;
       size = 0;
   }

   LinkedListX (E e){
       head = new Node(e);
       size = 1;
   }

   void add(E e){
       if (head == null){
           head = new Node (e);
       } else {
           Node current = this.head;
           while (current.next != null){
               current = current.next;
           }
           current.next = new Node(e);
       }
       size++;
   }

   E get (int n){
       if (n > size){
           return null;
       }
       Node current = head;
       for (int i=1;i<n;i++){
           current = current.next;
       }
       return current.e;
   }

   private class Node<E> {
       private E e;
       private Node next;

       Node (E e, Node n){
           this.e = e;
           this.next = n;
       }
       Node (E e) {
           this.e = e;
       }
   }

} }

the get() method gives error message get()方法给出错误消息

Incompatible types, Require:E, Found: java.lang.Object" at "return current.e 不兼容的类型,要求:E,Found:java.lang.Object“at”return current.e

I think I am using generics in a wrong way. 我想我是以错误的方式使用泛型。 Could someone let me know the right way to code this method? 有人能让我知道编码这种方法的正确方法吗?

Thanks. 谢谢。

As Node is an inner class it can also access the outer classes generic parameter. 由于Node是一个内部类,它还可以访问外部类泛型参数。 And you never assign a different value to E than the one from the outer class. 而且你永远不会为E分配不同于外层的值。 So just remove the <E> from Node class declaration: 所以只需从Node类声明中删除<E>

private class Node{
    // the rest
}

You're storing a Node as next without using its generic attributes - there are various places where you should be using Node<E> instead of just Node . 您将Node存储为next一个Node而不使用其通用属性 - 您应该使用Node<E>而不仅仅是Node

The problem basically is that you try to return E , but you save Node which is mapped by the compiler to Node<Object> - and Object isn't E 该问题主要是,你试图返回E ,但你保存Node是由编译器映射到Node<Object> -和ObjectE

To use correctly generic, you may change : 要正确使用通用,您可以更改:

  • in get() : Node current = head; in get()Node current = head; to Node<E> current = head; Node<E> current = head;
  • all your new Node() to new Node<>() (shortcut for new Node<E>() 所有new Node()new Node<>()new Node<E>()快捷方式
  • in Node class use also <E> Node类中也使用<E>

You can also just remove the <E> from all your Node (to all the Node class), you will still be able to use E for the data) 您也可以从所有Node删除<E> (到所有Node类),您仍然可以使用E作为数据)

You mix two problems: 你混合两个问题:

  1. This line causes the problem (4 lines above the type mismatch): 此行导致问题(类型不匹配上方4行):

     Node current = head; 

    You have to include generics as well, otherwise, its raw type is recognized as Object . 您还必须包含泛型,否则,其原始类型将被识别为Object

     Node<E> current = head; 

    Once you use a generic object, always include its type with <> . 使用通用对象后,请始终使用<>包含其类型。

  2. The second thing is that you already use generics on the outer class LinkedListX <E> thus all its inner work with the E parameter. 第二件事是你已经在外层类LinkedListX <E>上使用泛型,因此它的所有内部工作都使用了E参数。 For this, the generics within the Node class can be omitted. 为此,可以省略Node类中的泛型。

     private class Node { // the implementation } 

    Otherwise, the warning complaint appears: 否则,出现警告投诉:

    The type parameter E is hiding the type E 类型参数E隐藏了类型E.

Node current = head;

The above line is a raw type declaration, and in such case 'java.lang.Object' is the default Type Variable. 上面的行是原始类型声明,在这种情况下,'java.lang.Object'是默认的Type变量。

Using a parameterised type Node Node<E> current = head; 使用参数化类型节点Node<E> current = head; will solve the problem. 将解决问题。

Your code has a lot of raw references of Node which is generating a lot of warnings . 您的代码有很多Node的原始引用,它会产生很多警告。 In your get(int n) snippet Node is raw and not generic so Java type inference algorithm failed to identify it as E and considering its as an Object. 在你的get(int n)片段Node是原始的而不是通用的,因此Java类型推断算法无法将其识别为E并将其视为对象。 Please use the below code and check the difference in Eclipse 请使用以下代码并检查Eclipse中的差异

public class LinkedListX<E> {

    Node<E> head;
    int size;

    LinkedListX() {
        head = null;
        size = 0;
    }

    LinkedListX(E e) {
        head = new Node<E>(e);
        size = 1;
    }

    void add(E e) {
        if (head == null) {
            head = new Node<E>(e);
        } else {
            Node<E> current = this.head;
            while (current.next != null) {
                current = current.next;
            }
            current.next = new Node<E>(e);
        }
        size++;
    }

    E get(int n) {
        if (n > size) {
            return null;
        }
        Node<E> current = head;
        for (int i = 1; i < n; i++) {
            current = current.next;
        }
        return current.e;
    }

    static private class Node<E> {
        private E e;
        private Node<E> next;

        @SuppressWarnings("unused")
        Node(E e, Node<E> n) {
            this.e = e;
            this.next = n;
        }

        Node(E e) {
            this.e = e;
        }
    }
}

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

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