1

我正在创建一个通用的多路树,我知道它只会采用四种类型(整数、双精度、字符串和字符)中的一种。我在使用可比较的界面和插入功能时遇到问题。这是我到目前为止编写的相关代码:

public class BTree<T extends Comparable<? super T>>
{
    private int m=0, height=0, numkeys=0;
    private BTreeNode<T> root;
    boolean ready=false;

class BTreeNode<T extends Comparable<? super T>>
{
    boolean leaf = true;
    int keyTally = 1;
    Comparable<T> keys[] = new Comparable[m-1];
    BTreeNode<T> references[] = new BTreeNode[m];
    BTreeNode( T key)
    {
        keys[0] = (Comparable<T>) key;
        for ( int i=0; i<m; i++)
            references[i] = null;
    }
}

public BTree( int c)
{
    root=null;
    m=c;
}

// Not finished - can't test due to other bug
public boolean insertElement( T elem)
{
    insert( elem, root, height);
    numkeys++;
    return true;
}

// And now the ploblem function

private BTreeNode<T> insert( T elem, BTreeNode<T> node, int hvar)
{
    int i;

    BTreeNode<T> temp = null;

    if (hvar == 0)
    {
        for (i = 0; i < node.keyTally; i++)
            if ( elem.compareTo(node.keys[i]) < 0)
                break;
    }
    else
    {
        for (i = 0; i < node.keyTally; i++)
        {
            if ((i+1 == node.keyTally) || elem.compareTo(node.keys[i]) < 0)
            {
                Node n = insert( elem, node.references[i++], hvar-1);
                if (n == null)
                    return null;

                temp.keys[0] = n.keys[0];
                temp.references[0] = n;
                break;
            }
        }
    }

    for (int j = node.keyTally; j > i; j--)
        node.keys[j] = node.keys[j-1];
    node.children[i] = temp;
    node.keyTally++;
    if (node.keyTally < m)
        return null;
    else
    {
        BTreeNode<T> newNode = null;
        newNode.keyTally = m/2;
        for( i=0; i<m/2; i++)
        newNode.references[i] = node.references[m/2+i];
        return newNode;
    }
    return null;
}

这是我的控制台输出:

BTree.java:169: error: method compareTo in interface Comparable<T#2> cannot be applied
to given types;
if ( elem.compareTo(node.keys[i]) < 0)
         ^
required: CAP#1
found: Comparable<T#1>
reason: actual argument Comparable<T#1> cannot be converted to CAP#1 by method
invocation conversion
where T#1,T#2 are type-variables:
T#1 extends Comparable<? super T#1> declared in class BTree
T#2 extends Object declared in interface Comparable
where CAP#1 is a fresh type-variable:
CAP#1 extends Object super: T#1 from capture of ? super T#1

BTree.java:176: error: method compareTo in interface Comparable<T#2> cannot be
applied to given types;
if ((i+1 == node.keyTally) || elem.compareTo(node.keys[i]) < 0)
                                  ^
required: CAP#1
found: Comparable<T#1>
reason: actual argument Comparable<T#1> cannot be converted to CAP#1 by
method invocation conversion
where T#1,T#2 are type-variables:
T#1 extends Comparable<? super T#1> declared in class BTree
T#2 extends Object declared in interface Comparable
where CAP#1 is a fresh type-variable:
CAP#1 extends Object super: T#1 from capture of ? super T#1

BTree.java:178: error: cannot find symbol
Node n = insert( elem, node.references[i++], hvar-1);
^
symbol:   class Node
location: class BTree<T>
where T is a type-variable:
T extends Comparable<? super T> declared in class BTree

BTree.java:191: error: cannot find symbol
node.children[i] = temp;
    ^
symbol:   variable children
location: variable node of type BTree<T>.BTreeNode<T>
where T is a type-variable:
T extends Comparable<? super T> declared in class BTree

Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
4 errors

我不明白我在可比较接口上做错了什么,使它抱怨 compareTo 方法。另外,我想知道如果使用字符串类型会做什么?我认为它只会按字母顺序查看哪个先出现。谢谢 :)

4

2 回答 2

1

这是因为node.keys[i]是类型Comparable<T>,不是T。反过来尝试比较。

于 2012-04-07T16:56:50.700 回答
1

我建议您将 from 的类型更改为keysto Comparable<T>[]T[]因为这是您想要使用它的类型;您只需要小心不要将其返回到课堂之外);T或者您每次从中获得一些东西时手动转换为:(T)node.keys[i]

于 2012-04-07T19:25:08.003 回答