Java Reference
In-Depth Information
if (names[i].equals(name))
return values[i];
}
return null; // not found
}
// ...
}
Class IntegerLookup is not itself a generic class, rather it implements a
generic interface, providing a concrete type Integer in place of the ab-
stract type parameter T . Compared to the SimpleLookup class, this class
can store items in an array of Integer and find returns an Integer .
How would we modify processValues to work with generic implementa-
tions of Lookup ? Here's one attempt to do so:
void processValues(String[] names, Lookup<Object> table) {
for (int i = 0; i < names.length; i++) {
Object value = table.find(names[i]);
if (value != null)
processValue(names[i], value);
}
}
This compiles okay, and appears reasonable table is an instance of a
class that implements Lookup for any kind of Object . As Integer instances
are Object instances then we should be able to do this:
Lookup<Integer> l = new IntegerLookup();
// ... add entries to l ...
String[] names = { "One", "two" };
processValues(names, l); // INVALID: won't compile
 
Search WWH ::




Custom Search