繁体   English   中英

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

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

我是Java的新手,并尝试在Java中实现单链表。 我已经包含了泛型的使用。 代码如下所示:

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

}

get()方法给出错误消息

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

我想我是以错误的方式使用泛型。 有人能让我知道编码这种方法的正确方法吗?

谢谢。

由于Node是一个内部类,它还可以访问外部类泛型参数。 而且你永远不会为E分配不同于外层的值。 所以只需从Node类声明中删除<E>

private class Node{
    // the rest
}

您将Node存储为next一个Node而不使用其通用属性 - 您应该使用Node<E>而不仅仅是Node

该问题主要是,你试图返回E ,但你保存Node是由编译器映射到Node<Object> -和ObjectE

要正确使用通用,您可以更改:

  • in get()Node current = head; Node<E> current = head;
  • 所有new Node()new Node<>()new Node<E>()快捷方式
  • Node类中也使用<E>

您也可以从所有Node删除<E> (到所有Node类),您仍然可以使用E作为数据)

你混合两个问题:

  1. 此行导致问题(类型不匹配上方4行):

     Node current = head; 

    您还必须包含泛型,否则,其原始类型将被识别为Object

     Node<E> current = head; 

    使用通用对象后,请始终使用<>包含其类型。

  2. 第二件事是你已经在外层类LinkedListX <E>上使用泛型,因此它的所有内部工作都使用了E参数。 为此,可以省略Node类中的泛型。

     private class Node { // the implementation } 

    否则,出现警告投诉:

    类型参数E隐藏了类型E.

Node current = head;

上面的行是原始类型声明,在这种情况下,'java.lang.Object'是默认的Type变量。

使用参数化类型节点Node<E> current = head; 将解决问题。

您的代码有很多Node的原始引用,它会产生很多警告。 在你的get(int n)片段Node是原始的而不是通用的,因此Java类型推断算法无法将其识别为E并将其视为对象。 请使用以下代码并检查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