可比较的类型转换



我试图找到树内节点的最小值,为了检测是否有更小的值,我使用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<TreeNode> cannot be converted to TreeNode在if语句

我被告知对象必须强制转换为Comparable才能调用compareTo()

我试着看这个类似的问题,但是我没有权限改变TreeNode类

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) < 0),但产生相同的错误。

你知道如何正确地强制转换以下类吗?

根据其他人的建议,您可以使用可比的接口,这将要求您实现compareTo方法。

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

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

所以我们可以把你的类改成如下所示(注意:我建议将value转换为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必须实现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

最新更新