1

我正在尝试测试我编写的 java 泛型类,这是我的测试

public class BSTTest
{
    public void testInsert()
    {
        int height;
        BST<int> myTree = new BST<int>();
        myTree.insert(1);
    }

} 

但是当我编译时,我得到了意外类型的错误,它说如果找到一个 int 但需要在 BST myTree = new BST(); 行上引用 这意味着什么?

下面是我的二叉搜索树和节点类供参考

public class BST<E extends Comparable<E>>
{
    public Node<E> root;


    public BST()
    {
        root = null;
    }
    //insert delete find height
    public void find(E s, Node<E> n)
    {
        //empty tree, root is null
        if(n == null)
        {
            System.out.println("Item not present.");
        }
        //n is the node where s is, return n
        else if(n.getData().equals(s))
        {
            System.out.println("Item present");
        }
        //s is greater than n, look for s on the right subtree
        else if(s.compareTo(n.getData()) > 0)
        {
            find(s, n.getRight());
        }
        //s is less than n, look for s on the left subtree
        else
        {
            find(s, n.getLeft());
        }
    }

    public int height() 
    {
        int count;
        return count = height(root); 
    }

    private int height(Node<E> n)
    {
        int ct = 0;
        if(n == null)
        {

        }

        else
        {

            int left = height(n.getLeft());

            int right = height(n.getRight());

            ct = Math.max(left, right) + 1;
        }
        return ct;
    } 

    public void insert(E s) 
    {
        root = insert(s, root);
    } 

    private Node<E> insert(E s, Node<E> T)
    {
        //easiest case, empty tree, create new tree
        if(T == null)
        {
            T = new Node<E>(s,null,null);
        }
        //easiest case, found s
        else if(s.compareTo(T.getData()) == 0)
        {
            System.out.println("Item already present.");
        }
        //s is greater than T, insert on right subtree
        else if(s.compareTo(T.getData()) > 0)
        {
            T.setRight(insert(s, T.getRight()));
        }
        //s is less than T, insert on left subtree
        else
        {
            T.setLeft(insert(s,T.getLeft()));
        }
        return T;
    }

    public void delete(E d)
    {
    }

}

和我的节点类

public class Node<E> 
    {
       private E data;
    private Node<E> left;
    private Node<E> right;
    private Node<E> parent;

       public  Node(E d, Node<E> r, Node<E> l) 
    {
      data = d;

        left = l;
        right = r; 
       }
       public void setData(E d) 
    {
      data = d;
       }
    public E getData()
    {
        return data;
    }
       public Node<E> getRight() 
    {
      return right;
       }
    public void  setRight(Node<E> nd)
    {
        right = nd;
    }
       public Node<E> getLeft()
    {
        return left;
    }
    public void  setLeft(Node<E> nd)
    {
        left = nd;
    }
    public Node<E> getParent()
    {
        return parent;
    }
    public void  setParent(Node<E> nd)
    {
        parent = nd;
    }
}
4

4 回答 4

4

您可以尝试Integer代替int吗?

于 2013-02-26T07:17:39.403 回答
2

泛型类型只需要类(对象类型)而不是基本数据类型
它应该是

BST<Integer> myTree = new BST<Integer>();
于 2013-02-26T07:18:47.600 回答
0

Java 泛型仅适用于 Object 类型。因为,int是一个原始类型,所以你不能使用它。而是使用BST<Integer>

于 2013-02-26T07:19:02.233 回答
0

您不能将原始类型int用作 Java 中泛型类的参数。它必须是类类型,例如Integer.

于 2013-02-26T07:19:47.313 回答