2013-01-01 73 views
0

我正在開發一個基於模板的Java類實現各種樹結構(如標準二叉樹,紅黑樹或B樹)。我的想法是讓它像Java Collections中的各種列表一樣完成。這是一個接口類,然後由指定的樹進行擴展。不過,我打了一個奇怪的問題,在牆上:擴展類似的通用

BSTree.java:12: error: BSTree is not abstract and does not override abstract method  search(Comparable) in Tree 
public class BSTree<T extends Comparable<T>> extends Tree { 
    ^

BSTree.java:20: error: name clash: add(T#1) in BSTree and add(T#2) in Tree have the same erasure, yet neither overrides the other 
    public void add(T key) throws NullPointerException { 
       ^
    where T#1,T#2 are type-variables: 
    T#1 extends Comparable<T#1> declared in class BSTree 
    T#2 extends Comparable<T#2> declared in class Tree 

BSTree.java:28: error: method compareTo in interface Comparable<T#2> cannot be applied  to given types; 
       if (key.compareTo(ptr.key) == -1) { 
       ^
    required: T#1 
    found: Comparable 
    reason: actual argument Comparable cannot be converted to T#1 by method invocation conversion 
    where T#1,T#2 are type-variables: 
    T#1 extends Comparable<T#1> declared in class BSTree 
    T#2 extends Object declared in interface Comparable 

BSTree.java:42: error: name clash: remove(T#1) in BSTree and remove(T#2) in Tree have the same erasure, yet neither overrides the other 
    public void remove(T key) throws NullPointerException, TreeException { 
       ^
    where T#1,T#2 are type-variables: 
    T#1 extends Comparable<T#1> declared in class BSTree 
    T#2 extends Comparable<T#2> declared in class Tree 

BSTree.java:49: error: method compareTo in interface Comparable<T#2> cannot be applied  to given types; 
      if (key.compareTo(ptr.key) == 0) { 
       ^
    required: T#1 
    found: Comparable 
    reason: actual argument Comparable cannot be converted to T#1 by method invocation conversion 
    where T#1,T#2 are type-variables: 
    T#1 extends Comparable<T#1> declared in class BSTree 
    T#2 extends Object declared in interface Comparable 

BSTree.java:81: error: method compareTo in interface Comparable<T#2> cannot be applied to given types; 
      else if (key.compareTo(ptr.key) < 0) ptr = ptr.left; 
         ^
    required: T#1 
    found: Comparable 
    reason: actual argument Comparable cannot be converted to T#1 by method invocation conversion 
    where T#1,T#2 are type-variables: 
    T#1 extends Comparable<T#1> declared in class BSTree 
    T#2 extends Object declared in interface Comparable 

BSTree.java:89: error: name clash: search(T#1) in BSTree and search(T#2) in Tree have  the same erasure, yet neither overrides the other 
    public Node<T> search(T key) throws NullPointerException, KeyNotStoredException { 
       ^
    where T#1,T#2 are type-variables: 
    T#1 extends Comparable<T#1> declared in class BSTree 
    T#2 extends Comparable<T#2> declared in class Tree 

BSTree.java:94: error: method compareTo in interface Comparable<T#2> cannot be applied to given types; 
      if (key.compareTo(ptr.key) == 0) return ptr; 
       ^
    required: T#1 
    found: Comparable 
    reason: actual argument Comparable cannot be converted to T#1 by method invocation conversion 
    where T#1,T#2 are type-variables: 
    T#1 extends Comparable<T#1> declared in class BSTree 
    T#2 extends Object declared in interface Comparable 

BSTree.java:95: error: method compareTo in interface Comparable<T#2> cannot be applied to given types; 
      else if (key.compareTo(ptr.key) < 0) ptr = ptr.left; 
        ^
    required: T#1 
    found: Comparable 
    reason: actual argument Comparable cannot be converted to T#1 by method invocation conversion 
    where T#1,T#2 are type-variables: 
    T#1 extends Comparable<T#1> declared in class BSTree 
    T#2 extends Object declared in interface Comparable 

它看起來像Java認爲,對象是不同類型的...如何解決呢?

下面是一段我的代碼:

Tree.java

class Node<T extends Comparable<T>> { 

    protected T key; 
    protected Node parent, left, right; 

    public Node(T key, Node parent) { 
     this.key = key; 
     this.parent = parent; 
     this.left = null; 
     this.right = null; 
    } 

} 

public abstract class Tree<T extends Comparable<T>> { 
    protected Node<T> root; 
protected Integer nodesCount; 

    public abstract void add(T key) throws NullPointerException; 

    public abstract void remove(T key) throws NullPointerException, TreeException; 

    public abstract Node<T> search(T key) throws NullPointerException, KeyNotStoredException; 
} 

BSTree.java

public class BSTree<T extends Comparable<T>> extends Tree { 

    public BSTree() { 
     root = null; 
     nodesCount = new Integer(0); 
    } 

    @Override 
    public void add(T key) throws NullPointerException { 
     if (root == null) root = new Node<T>(key, null);  
     else {  
      boolean left = false; 
      Node ptr = root, parent = ptr.parent; 
      while (ptr != null) { 
       parent = ptr; 
       left = false; 
       if (key.compareTo(ptr.key) == -1) { 
        ptr = ptr.left; 
        left = true; 
       } else ptr = ptr.right; 
      } 

      if (left) parent.left = new Node<T>(key, parent); 
      else parent.right = new Node<T>(key, parent); 
     } 

     nodesCount++; 
    } 

    @Override 
    public void remove(T key) throws NullPointerException, TreeException { 
     /* implementation */ 
    } 

    @Override 
    public Node<T> search(T key) throws NullPointerException, KeyNotStoredException { 
     /* implementation */ 
    } 

} 

編輯: 感謝您的諮詢件我能將錯誤數量減少到5.這裏是: 的javac -d ../bin *的.java

BSTree.java:28: error: method compareTo in interface Comparable<T#2> cannot be applied to given types; 
       if (key.compareTo(ptr.key) == -1) { 
        ^
    required: T#1 
    found: Comparable 
    reason: actual argument Comparable cannot be converted to T#1 by method invocation conversion 
    where T#1,T#2 are type-variables: 
    T#1 extends Comparable<T#1> declared in class BSTree 
    T#2 extends Object declared in interface Comparable 

BSTree.java:49: error: method compareTo in interface Comparable<T#2> cannot be applied to given types; 
      if (key.compareTo(ptr.key) == 0) { 
      ^
    required: T#1 
    found: Comparable 
    reason: actual argument Comparable cannot be converted to T#1 by method invocation conversion 
    where T#1,T#2 are type-variables: 
    T#1 extends Comparable<T#1> declared in class BSTree 
    T#2 extends Object declared in interface Comparable 

BSTree.java:81: error: method compareTo in interface Comparable<T#2> cannot be applied to given types; 
      else if (key.compareTo(ptr.key) < 0) ptr = ptr.left; 
         ^
    required: T#1 
    found: Comparable 
    reason: actual argument Comparable cannot be converted to T#1 by method invocation  conversion 
    where T#1,T#2 are type-variables: 
    T#1 extends Comparable<T#1> declared in class BSTree 
    T#2 extends Object declared in interface Comparable 

BSTree.java:94: error: method compareTo in interface Comparable<T#2> cannot be applied to given types; 
      if (key.compareTo(ptr.key) == 0) return ptr; 
       ^
    required: T#1 
    found: Comparable 
    reason: actual argument Comparable cannot be converted to T#1 by method invocation conversion 
    where T#1,T#2 are type-variables: 
    T#1 extends Comparable<T#1> declared in class BSTree 
    T#2 extends Object declared in interface Comparable 

BSTree.java:95: error: method compareTo in interface Comparable<T#2> cannot be applied to given types; 
      else if (key.compareTo(ptr.key) < 0) ptr = ptr.left; 
         ^
    required: T#1 
    found: Comparable 
    reason: actual argument Comparable cannot be converted to T#1 by method invocation conversion 
    where T#1,T#2 are type-variables: 
    T#1 extends Comparable<T#1> declared in class BSTree 
    T#2 extends Object declared in interface Comparable 

現在,我的代碼有Node<T>Tree<T>它缺乏它。但是還有什麼問題呢?

+0

您可能需要擴展'Tree ',而不是'Tree' ... –

+0

您使用新的錯誤消息編輯沒有匹配的代碼,所以很難說,但它看起來像'ptr'仍然被聲明爲'Node'而不是'Node '。 –

回答

7

當您在JDK複製功能,你應該閱讀代碼來得到一些想法。

您的代碼需要使用Node和Tree泛型。

public class BSTree<T extends Comparable<T>> extends Tree<T> { 

protected Node<T> parent, left, right; 

BTW:你不應該使用的包裝時,你可以使用原始的。

protected int nodesCount; 
+2

也可以在'BSTree#add(T)'方法中使用'Node ptr = root,parent = ptr.parent;'。 –

+0

謝謝它的工作 – Robin92

0

你缺少Tree泛型參數在BSTree聲明:

public class BSTree<T ...> extends Tree<T> 

這意味着BSTreeadd(T)方法不會覆蓋一個在Tree,因爲它是,因爲他們沒有相同的參數類型。

然而,由於T作爲一類不是更精確的比Object(我們只知道它實現了Comparable接口),這兩種方法具有相同的擦除作爲add(Object),具有潛在的不兼容的類型(在所識別的編譯器的錯誤輸出爲T#1T#2)。

0

嘗試:

public class BSTree<T extends Comparable<T>> extends Tree<T> {