无法查找包含Java的方法

时间:2016-05-22 02:57:03

标签: java compilation contains

当我尝试编译代码时,编译器找不到contains方法。这是我的代码中无法找到的地方:

    public boolean search(T target) {
    if(isEmpty()) return false;
    if(root.equals(target))return true;
    if(left != null && left.contains(target)) return true;
    if(right != null && right.contains(target)) return true;
    return false;
}

代码是在二进制搜索树中查找值,但编译器无法找到contains方法。

也许包含方法不支持对象类型,这也就是它给出错误的原因?这个方法是我老师的一个例子,应该是兼容的,所以我很困惑为什么我会收到这个错误。

以下是构造函数:

public class LinkedBST<T extends Comparable<T>> {

/** The element held at the root of this tree. */
private T root;

/** The left subtree of this tree. */
private LinkedBST<T> left;

/** The right subtree of this tree. */
private LinkedBST<T> right;

    /**                                                                                                                                                                                                                                       
     *  Creates a BST with the given value.                                                                                                                                                                                                   
     *                                                                                                                                                                                                                                        
     * @param value to store at the root of this LinkedBST.                                                                                                                                                                                   
     */
    public LinkedBST(T value) {
        root = value;
        left = null;
        right = null;
    }

    /**                                                                                                                                                                                                                                       
     *  Creates a new empty BST.                                                                                                                                                                                                              
     */
    public LinkedBST() {
        this(null);
    }

谢谢,如果你需要搜索整个代码,它就在这里: http://pastebin.com/UERFa7kQ

或者,如果你需要一个特定的位只是评论,我认为错误可能来自上面,所以我只是把它们放进去,我不想要垃圾邮件。

谢谢,

[编辑]

这是我不断收到的错误,抱歉没有提前发布!

LinkedBST.java:94: error: cannot find symbol
    if(left != null && left.contains(target)) return true;
                           ^
     symbol:   method contains(T)
     location: variable left of type LinkedBST<T>
     where T is a type-variable:
     T extends Comparable<T> declared in class LinkedBST
 LinkedBST.java:95: error: cannot find symbol
     if(right != null && right.contains(target)) return true;
                             ^
     symbol:   method contains(T)
     location: variable right of type LinkedBST<T>
     where T is a type-variable:
    T extends Comparable<T> declared in class LinkedBST
2 errors

1 个答案:

答案 0 :(得分:1)

您没有实施contains方法。实施它,一切都会好的。

相关问题