【问题标题】:Casting with Comparable与可比铸造
【发布时间】:2021-02-11 04:52:22
【问题描述】:

我试图在树中找到一个节点的最小值,为了检测是否有较小的值,我正在使用compareTo() 函数,如下所示:

   @SuppressWarnings("unchecked")
   public static Object min(TreeNode t)
   {
      if(t == null) {
         return null;
      }
      
      Comparable<TreeNode> min = (Comparable<TreeNode>) t;
      
      if(t.getLeft() != null) {
         Comparable<TreeNode> leftMin = (Comparable<TreeNode>) min(t.getLeft());
         
         if( ((Comparable<TreeNode>)leftMin).compareTo( (Comparable<TreeNode>)min) < 0) {
            min = leftMin;
         }
      }
      
      if(t.getRight() != null) {
         Comparable<TreeNode> rightMin = (Comparable<TreeNode>) min(t.getRight());
         
         if( ((Comparable<TreeNode>)rightMin).compareTo( (Comparable<TreeNode>)min) < 0) {
            min = rightMin;
         }
      }
      
      return min;
   }

但是,我收到以下错误: error: incompatible types: Comparable&lt;TreeNode&gt; cannot be converted to TreeNode 在 if 语句中。

有人告诉我必须将 Object 强制转换为 Comparable 才能调用 compareTo()

我已经尝试查看这个类似的question,但我无权更改 TreeNode 类

树节点类:

public class TreeNode
{
   private Object value; 
   private TreeNode left, right;
   
   public TreeNode(Object initValue)
   { 
      value = initValue; 
      left = null; 
      right = null; 
   }

   /*methods*/

}

我也尝试过:if(leftMin.compareTo(min) &lt; 0) 但是会产生相同的错误。

您知道如何正确转换和转换以下类吗?

【问题讨论】:

  • 如果你正确使用泛型,你根本不需要任何转换。

标签: java casting tree binary-tree comparable


【解决方案1】:

按照其他人的建议,您可以使用可比较的接口,这需要您实现compareTo 方法。

可以在 java se 文档中找到与实现细节的比较:

将此对象与指定对象进行比较以进行排序。返回一个 负整数、零或正整数,因为此对象较小 大于、等于或大于指定对象。

因此我们可以将您的类更改为如下所示的内容(注意:我建议将值转换为 int 或任何其他原始类型):

 class TreeNode implements Comparable<TreeNode> {
        // recommend to convert value to int or any other primitive type
        private Object value; 
        private TreeNode left, right;
       
        public TreeNode(Object initValue) { 
            value = initValue; 
            left = null; 
            right = null; 
        }
       
        // if value is int, just use ==, < and > 
        // i.e. this.value == o.value, this.value < o.value and so on ...
        @Override
        public int compareTo(TreeNode o) {
            if (this.value.equals(o.value)) return 0;
            else if (this.value.hashCode() < o.value.hashCode()) return -1;
            else return 1;
        }
    
       /*methods*/
}

那么你真的不需要在min 方法中进行转换。 (注意:下面的实现实际上并不正确 - 不会给你最小值。它只是显示在实现可比较的接口后实现将如何变化)。

// This method is not actually correct (i.e. won't actually find the min), 
// but showing how it would change after using the comparable interface 
// on the tree node class. 
public TreeNode min(TreeNode t) {
    if(t == null) {
     return null;
    }
  
    TreeNode min =  t;
  
    if(t.getLeft() != null) {
        TreeNode leftMin = min.getLeft();
     
        if(leftMin.compareTo(min) < 0) {
            min = leftMin;
        }
    }
  
    if(t.getRight() != null) {
        TreeNode rightMin = min.getRight();
        
        if( rightMin.compareTo(min) < 0) {
            min = rightMin;
        }
    }
  
    return min;
}

【讨论】:

  • 我无法更改课程
  • 在这种情况下,尝试扩展TreeNode 类(使用extends 关键字对其进行子类化)或使用TreeNode 作为成员变量创建一个新类,然后实现comparable该类的接口。根据该新类编写您的最小查找逻辑。
【解决方案2】:

TreeNode 类必须实现Comparable 接口:

public class TreeNode implements Comparable<TreeNode> {
    ...
    @Override
    public int compareTo(TreeNode other) {
        ...  // maybe compare 'initValue' here
    }
}

也不确定将TreeNode 转换为Comparable 是否很好,我更愿意工作 完全不强制转换(例如public static TreeNode min(TreeNode t)


编辑:可能是为了将 initValue 转换为 Comparable 以便进行比较 - 在​​这种情况下将其声明为 Object 不是很安全(类型)。


问题更改后编辑。由于TreeNode无法更改,我认为您必须清楚地拆分节点和值,可能如下所示(必须完成):

public static TreeNode min(TreeNode node) {
    ...
    TreeNode minNode = node;
    Comparable<?> minValue = (Comparable<?>) minNode.getValue(); // guessed method name

    if (node.getLeft() != null) {
        TreeNode leftMin = min(t.getLeft());
        Comparable<?> leftValue = (Comparable<?>) leftMin.getValue();
        if (leftValue.compareTo(minValue) < 0) {
            minNode = leftNode;
            minValue = leftValue;
        }
    ...

这要求Value 中的实例实现Comparable

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2012-06-05
  • 1970-01-01
  • 1970-01-01
  • 2014-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多