警告:unchecked调用compareTo(T)作为原始类型java.lang.Comparable的成员

时间:2013-08-09 09:00:35

标签: java generics

我正在创建一个使用二叉搜索树实现泛型Set的类。我在几个方法中使用“compareTo”方法,无论我做什么,我都会不断收到声明。任何帮助表示赞赏!

// Allow short name access to following classes
import csc143.data_structures.*;

public class MySet<E> implements SimpleSet<E> {

  // the root of the "tree" that structures the set
  private BTNode root;
  // the current number of elements in the set
  private int numElems;

  public MySet() {
    root = null;

    numElems = 0;
  }

  /**
   * Add an element to the set. 
   * 
   * @param e The element to be added to the set.
   * @return  <tt>true</tt> If this operation updated the contents of the set.
   */
  public boolean add(E e) {
    try {
      root = addToSubtree(root, (Comparable) e);
      return true;
    } catch(DuplicateAdded exc) {
      // duplicate trying to be added
      return false;
    }

  }

  // This helper method adds the element "e" to tree rooted at r. Returns
  // (possibly new) tree containing "e", or throws DuplicateAdded exception
  // if "e" already exists in tree.
  private BTNode addToSubtree(BTNode r, Comparable elem)
    throws DuplicateAdded {
    if(r == null) {
      return new BTNode(elem);
    }

    int compare = elem.compareTo(r.item);
    // element already in tree
    if(compare == 0) {
      throw new DuplicateAdded("Element is already in set");
    }
    if(compare < 0) {
      r.left = addToSubtree(r.left, elem);
    } else {  // compare > 0
      r.right = addToSubtree(r.right, elem);
    }

    // element has been added
    return r;
  }

  /**
   * Remove all elements from this set.
   */
  public void clear() {
    root = null;

    numElems = 0;
  }

  /**
   * Checks for the existance of the specified value within the set.
   * 
   * @param e The value sought.
   * @return  <tt>true</tt> If the value exists in the set.
   */
  public boolean contains(E e) {
    return subtreeContains(root, (Comparable) e);
  }

  // This helper method returns whether element "elem" is in
  // (sub-)tree with root "r".
  private boolean subtreeContains(BTNode r, Comparable elem) {
    if(r == null) {
      return false;
    } else {
      int compare = elem.compareTo(r.item);
      // found element
      if(compare == 0){
        return true;
      } else if(compare < 0) {
        return subtreeContains(r.left, elem);
      } else {  // compare > 0
        return subtreeContains(r.right, elem);
      }

    }

  }

  /**
   * Check for the existance of elements in the set.
   * 
   * @return  <tt>true</tt> If there are no elements in the set.
   */
  public boolean isEmpty() {
    return root == null;
  }

  /**
   * Return the number of elements in the set.
   * 
   * @return The number of elements in the set.
   */
  public int size() {
    return numElems;
  }

  /**
   * Returns a String representation of the contents of the set.
   * 
   * @return  The String representation of the set.
   */
  public String toString() {

  }

  // this inner class creates the node that compose the binary tree structure
  class BTNode<E> {

    /**
     * The item stored in the node.
     */
    public E item;

    /**
     * The node to the left of "this" node.
     */
    public BTNode left;

    /**
     * The node to the right of "this" node.
     */
    public BTNode right;

    /**
     * Constructs the BTNode object (three parameters).
     * 
     * @param item The item to be stored in the node.
     * @param left The node to the left of "this" node.
     * @param right The node to the right of "this" node.
     */
    @SuppressWarnings("unchecked")
    public BTNode(Object item, BTNode left, BTNode right) {
      // bind to references
      this.item = (E) item;
      this.left = left;
      this.right = right;
    }

    /**
     * Constructs the BTNode (one parameter).
     * 
     * @param The item to be stored in the node.
     */
    public BTNode(Object item) {
      // call three parameter constructor
      this(item, null, null);
    }

  }

}

编辑:包括SimpleSet界面:

package csc143.data_structures;

public interface SimpleSet<E> {

  /**
   * Add an element to the set. 
   * 
   * @param e The element to be added to the set.
   * @return  <tt>true</tt> If this operation updated the contents of the set.
   */
  public boolean add(E e);

  /**
   * Remove all elements from this set.
   */
  public void clear();

  /**
   * Checks for the existance of the specified value within the set.
   * 
   * @param e The value sought.
   * @return  <tt>true</tt> If the value exists in the set.
   */
  public boolean contains(E e);

  /**
   * Check for the existance of elements in the set.
   * 
   * @return  <tt>true</tt> If there are no elements in the set.
   */
  public boolean isEmpty();

  /**
   * Return the number of elements in the set.
   * 
   * @return The number of elements in the set.
   */
  public int size();

  /**
   * Returns a String representation of the contents of the set.
   * 
   * @return  The String representation of the set.
   */
  public String toString();

}

2 个答案:

答案 0 :(得分:2)

方法上的签名使用原始Comparable接口而不使用泛型。您的集合实现中似乎需要数据类型实现Comparable,因此,由于您现在使用泛型,因此您应该全面地进行更改。

您没有发布SimpleSet的类声明,因此Comparable上的E限制可能已存在。如果没有,您需要将类声明更改为:

public class MySet<E extends Comparable<? super E>> implements SimpleSet<E>

这告诉您的类的客户端,只允许实现Comparable的类型作为集合实现的泛型类型参数。您没有发布BTNode的代码,但也可能需要在EBTNode<E>)上进行参数化。

现在,由于您只允许将E类型的对象添加到集合中,因此您应该更改您的加法器方法以反映:

private BTNode<E> addToSubtree(BTNode<E> r, E elem) throws DuplicateAdded

依此类推,对于subtreeContains等,Java泛型的要点是你用类型占位符(Comparable替换所有那些强制转换(对于你的情况为E) for element)在编译时限制可以添加的内容,因此最需要显式转换。泛型是一个功能强大但功能复杂的功能,我建议阅读the official tutorial

答案 1 :(得分:0)

执行elem.compareTo(r.item);

elem的类型为Comparable

r的类型为BTNode

这就是警告,编译器抱怨的原因,因为elem 可以是其他东西而不是BTNode实例。

理想情况下,您的private BTNode addToSubtree(BTNode r, Comparable elem)应该同时使用两种类型的参数。或者你可以使用以下内容进行创可贴:

if(elem instanceof BTNode){
  elem.compareTo(r.item);
}