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));
}
}

The above is a class declaration for a table abstract data type that my professor was going over in class. I've been trying to figure out why does Java give me the following error message

Type arguments given on raw type

When I declare my variable here

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

What are raw types in java? We've discussed writing generic classes. Is it related to that?

j.jerrod.taylor
  • 1,120
  • 1
  • 13
  • 33
  • It would be a lot easier to read this if you followed the convention of giving type parameters 1-character names (`K`, `V`). Without that it's difficult to spot where you're referencing a real class and where you're using a parameter. – Mark Peters Mar 01 '13 at 15:09
  • You haven't shown us what actually matters: the definition of the class `BST`. – Mark Peters Mar 01 '13 at 15:10
  • @MarkPeters: Great! Bring back single-character identifiers - just what we need to improve code readability. – Adrian Pronk Jul 02 '13 at 03:37
  • @AdrianPronk: There is no "bring back." Whether you agree with it or not, it's already the extremely well established [Java convention](http://docs.oracle.com/javase/tutorial/java/generics/types.html). Following conventions enhances readability in this case over using a descriptive name (which appears to suggest that it's not a type parameter but rather a class name). – Mark Peters Jul 02 '13 at 03:41

2 Answers2

4

Your table class requires generic parameters, but you are referencing it as a raw type here:

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

You need to supply generic parameters for it, as so:

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

And as others have pointed out, its standard to define generic parameters as one letter characters, so K instead of Key, and V instead of Value. It makes it substantially easier to read and understand your code, and helps avoid confusion with real type names.

Perception
  • 79,279
  • 19
  • 185
  • 195
  • Thanks. That worked. But I still don't really understand why you have to do it that way? Why do I specifically have to call Table.Entry and not just use Entry? Shouldn't anything that is in Entry be available to table since Entry is an inner class of Table? – j.jerrod.taylor Mar 01 '13 at 15:47
  • @j.jerrod.taylor - Absolutely. You can actually remove the `Table` portion entirely, all its doing is providing scope thats not needed. – Perception Mar 01 '13 at 15:50
0

Entry is a raw type, while Entry<Key, Value> is not. o, in your code, the snippet :

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

is including a raw type.

Fabien
  • 12,486
  • 9
  • 44
  • 62
  • You know, you can flag the question as duplicated instead of giving a very simple explanation contrasted by the answer in the possible duplicated link. – Luiggi Mendoza Mar 01 '13 at 15:14