简体   繁体   English

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

[英]Warning: unchecked call to compareTo(T) as a member of the raw type java.lang.Comparable

I am creating a class that implements a generic Set using a binary search tree. 我正在创建一个使用二叉搜索树实现泛型Set的类。 I use the "compareTo" method in a couple of my methods, and I keep getting the stated warning regardless of what I do. 我在几个方法中使用“compareTo”方法,无论我做什么,我都会不断收到声明。 Any help is appreciated! 任何帮助表示赞赏!

// 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);
    }

  }

}

EDIT : includes SimpleSet interface: 编辑 :包括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();

}

The signature on your method uses the raw Comparable interface without the generic. 方法上的签名使用原始的Comparable接口而不使用泛型。 There appears to be a requirement in your set implementation that the data types implement Comparable , so since you're now using generics, you should make the change comprehensively. 在您的集合实现中似乎需要数据类型实现Comparable ,因此,由于您现在使用泛型,因此您应该全面地进行更改。

You didn't post the class declaration for SimpleSet , so the Comparable restriction on E may already be there. 您没有发布SimpleSet的类声明,因此E上的Comparable限制可能已存在。 If not, you need to change your class declaration to: 如果没有,您需要将您的类声明更改为:

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

This tells clients of your class that only types implementing Comparable are permitted as generic type parameters for your set implementation. 这告诉您的类的客户端,只允许实现Comparable类型作为set实现的泛型类型参数。 You didn't post the code for BTNode , but it probably needs to be parameterized on E as well ( BTNode<E> ). 你没有发布BTNode的代码,但它也可能需要在E上进行参数化( BTNode<E> )。

Now, since you're only permitting objects of type E to be added to the set, you should change your adder method to reflect that: 现在,由于您只允许将类型为E对象添加到集合中,因此您应该更改您的加法器方法以反映:

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

And so on, for subtreeContains , etc. The point of Java generics is that you replace all those casts everywhere (to Comparable , in your case) with type placeholders ( E for element) that limit at compile time what can be added and so do away with most need for explicit casting. 等等,对于subtreeContains等.Java泛型的要点是你用类型占位符(元素为E )替换所有那些转换(在你的情况下为Comparable ),它们在编译时限制可以添加的内容等等。最需要明确的铸造。 Generics are a powerful but complicated feature, and I recommend reading the official tutorial . 泛型是一个功能强大但功能复杂的功能,我建议阅读官方教程

When you do elem.compareTo(r.item); 当你做elem.compareTo(r.item);

elem is of type Comparable elem属于可Comparable类型

r is of type BTNode rBTNode类型

That's is why the warning, compiler is complaining because elem can be something else and not a BTNode instance. 这就是警告,编译器抱怨的原因,因为elem 可能是其他东西而不是BTNode实例。

Ideally your private BTNode addToSubtree(BTNode r, Comparable elem) should take both params of same type. 理想情况下,您的private BTNode addToSubtree(BTNode r, Comparable elem)应该采用相同类型的两个参数。 Or you can band-aid fix it with: 或者您可以使用以下内容进行创可贴修复:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java“unchecked调用compareTo(T)作为原始类型java.lang.Comparable的成员” - Java “unchecked call to compareTo(T) as a member of the raw type java.lang.Comparable” java.lang.Comparable中的compareTo(K) <K> 不能应用于(java.lang.Comparable) - compareTo(K) in java.lang.Comparable<K> cannot be applied to (java.lang.Comparable) 原始类型为“ java.lang.Iterable”的成员对“ forEach()”的未经检查的调用 - Unchecked call to 'forEach()' as a member of raw type 'java.lang.Iterable' 警告:[未选中]未选中调用添加(E)作为原始类型java.util.List的成员 - warning: [unchecked] unchecked call to add(E) as a member of the raw type java.util.List java.lang.Comparable和equals - java.lang.Comparable and equals 将未经检查的调用修复为原始类型警告Java - Fixing unchecked call to raw type warning Java Java:unchecked调用compareTo(T) - Java: unchecked call to compareTo(T) 如何解决错误:“ AlphaSorter必须实现继承的抽象方法java.lang.Comparable <AlphaSorter> .compareTo(AlphaSorter)? - How to fix the error: " AlphaSorter must implement the inherited abstract method java.lang.Comparable<AlphaSorter>.compareTo(AlphaSorter)? Java反射:避免警告未经检查的调用getConstructor(Class <?> …)作为原始类型Class的成员 - java reflection :Avoid warning unchecked call to getConstructor(Class<?>…) as a member of the raw type Class 编译警告:未选中调用XXX作为原始类型的成员 - Compilation warning: Unchecked call to XXX as member of the raw type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM