3

二分探索木を使用してジェネリック 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);
    }

  }

}

EDIT: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();

}
4

2 に答える 2

2

メソッドの署名はComparable、ジェネリックなしで生のインターフェイスを使用します。セットの実装には、データ型が実装するという要件があるComparableようです。そのため、ジェネリックを使用しているため、包括的に変更する必要があります。

のクラス宣言を投稿していないSimpleSetため、 のComparable制限Eが既に存在する可能性があります。そうでない場合は、クラス宣言を次のように変更する必要があります。

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

Comparableこれにより、セット実装のジェネリック型パラメーターとして実装する型のみが許可されることをクラスのクライアントに伝えます。のコードは投稿していませんBTNodeが、おそらくパラメーター化する必要がありますE( BTNode<E>)。

ここで、タイプのオブジェクトのみをEセットに追加できるようにしているため、それを反映するように adder メソッドを変更する必要があります。

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

など、 forなど。Javaジェネリックのポイントは、コンパイル時に追加できるものを制限し、ほとんどを廃止する型プレースホルダー( for 要素)で、subtreeContainsどこでも(あなたの場合は to に)キャストをすべて置き換えることです。明示的なキャストが必要です。ジェネリックは強力ですが複雑な機能です。公式のチュートリアルを読むことをお勧めします。ComparableE

于 2013-08-09T09:15:41.333 に答える
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);
}
于 2013-08-09T09:19:50.847 に答える