data structures - Why does java complain about raw types in my variable declaration? -
this question has answer here:
- what raw type , why shouldn't use it? 12 answers
public class table<key extends comparable<key>, value> { /* * purpose of entry glue key , value * * class use key has 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 3 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 delete method bst yet. return tree.delete(new entry(k, null)); } } the above class declaration table abstract data type professor going on in class. i've been trying figure out why java give me following error message
type arguments given on raw type
when declare variable here
private bst<table.entry<key, value>> tree = new bst<table.entry<key, value>>(); what raw types in java? we've discussed writing generic classes. related that?
your table class requires generic parameters, referencing raw type here:
private bst<table.entry> tree = new bst<table.entry>();
you need supply generic parameters it, so:
private bst<table<key, value>.entry<key, value>> tree = new bst<table<key, value>.entry<key, value>>(); and others have pointed out, standard define generic parameters 1 letter characters, k instead of key, , v instead of value. makes substantially easier read , understand code, , helps avoid confusion real type names.
Comments
Post a Comment