/** * Returns the value associated with the given key in this symbol table. * Takes advantage of the fact that the keys appear in increasing order to terminate * early when possible. * * @param key the key * @return the value associated with the given key if the key is in the symbol table * and {@code null} if the key is not in the symbol table * @throws IllegalArgumentException if {@code key} is {@code null} */ public Value get(Key key) { // TODO // Change this code to make use of the fact the list is sorted to terminate early // when possible. if (key == null) throw new IllegalArgumentException("argument to get() is null"); for (Node x = first; x != null; x = x.next) { if (key.equals(x.key)) return x.val; } return null; } You may not use the keys() method. Note: You are modifying the implementation of the methods, but not their interface or contract.
/**
* Returns the value associated with the given key in this symbol table.
* Takes advantage of the fact that the keys appear in increasing order to terminate
* early when possible.
*
* @param key the key
* @return the value associated with the given key if the key is in the symbol table
* and {@code null} if the key is not in the symbol table
* @throws IllegalArgumentException if {@code key} is {@code null}
*/
public Value get(Key key) {
// TODO
// Change this code to make use of the fact the list is sorted to terminate early
// when possible.
if (key == null) throw new IllegalArgumentException("argument to get() is null");
for (Node x = first; x != null; x = x.next) {
if (key.equals(x.key))
return x.val;
}
return null;
}
You may not use the keys() method. Note: You are modifying the implementation of the methods, but not their interface or contract.
Step by step
Solved in 4 steps with 5 images