簡體   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