1
public class Table<Key extends Comparable<Key>, Value> {

/*
* The purpose of entry is to glue together a key and a value
*
* The class that we use for Key has to implement comparable
* */
private class Entry<Key extends Comparable<Key>, Value> implements Comparable<Entry>
{
    Key key;
    Value value;

    public Entry(Key k, Value v)
    {
        key = k;
        value = v;
    }

    public int compareTo(Entry<Key,Value> entry)
    {
        return key.compareTo(entry.key);
    }
}

private BST<Table.Entry<Key, Value>> tree = new BST<Table.Entry<Key, Value>>();

//must supply public methods for the three operations

public Value lookUp(Key key)
{
    Entry<Key, Value> e = new Entry<Key, Value>(key, null);

    return tree.search(e).value;
}

public boolean insert(Key k, Value v)
{
    return tree.insert(new Entry<Key, Value>(k, v));
}

public boolean delete(Key k)
{
    //we haven't written a delete method for bst yet.
    return tree.delete(new Entry(k, null));
}
}

以上是我的教授在课堂上讨论的表抽象数据类型的类声明。我一直在试图弄清楚为什么 Java 会给我以下错误消息

在原始类型上给出的类型参数

当我在这里声明我的变量时

private BST<Table.Entry<Key, Value>> tree = new BST<Table.Entry<Key, Value>>();

java中的原始类型是什么?我们已经讨论过编写泛型类。与此有关吗?

4

2 回答 2

4

您的表类需要通用参数,但您在此处将其作为原始类型引用:

私有 BST< Table .Entry> 树 = 新 BST< Table .Entry>();

您需要为其提供通用参数,如下所示:

private BST<Table<Key, Value>.Entry<Key, Value>> tree = 
    new BST<Table<Key, Value>.Entry<Key, Value>>();

正如其他人所指出的那样,其标准将泛型参数定义为一个字母字符,因此K代替Key, 而V代替Value. 它使阅读和理解代码变得更加容易,并有助于避免与真实类型名称混淆。

于 2013-03-01T15:15:00.537 回答
0

Entry是原始类型,而Entry<Key, Value>不是。o,在您的代码中,代码段:

public boolean delete(Key k)
{
    //we haven't written a delete method for bst yet.
    return tree.delete(new Entry(k, null));
}

包括原始类型。

于 2013-03-01T15:11:52.617 回答