Exercise 13.x1: The following definition of a get
method is intended to be inserted into the IntList
class. Replace the TODO comment with Java code that makes the method behave as specified. Your procedure should be efficient in the case of a small index within a long list.
/** * Get an element from this IntList by position. * @param index the position (counting from 0) * @return the value at that position * @throws IndexOutOfBoundsException if index not in [0,length) */ public int get(int index){ // TODO handle the non-exceptional case throw new IndexOutOfBoundsException(); }
Do Exercise 14.1 on pages 265-266. By using the cleaned-up HeapManager
from our course web site, you can confine your changes to the findBlockPredecessor
method.
You will need to supplement the code sequences in the two hints with appropriate types for the variables. The first line in each code sequence can be of the form
HeapManager mm = new HeapManager(new int[...]);
Similarly, the variables a
, b
, and c
need to be declared to be of type int
in the second code sequence the same way as in the first.
A parenthetical note in the exercise says that you should use the version of HeapManager
that include the coalescing deallocate
. If you would rather use the non-coalescing version, then interchange the last two lines of each of the code sequences. The hints are valid for both the coalescing and the non-coalescing version if you put these two lines in this order:
mm.deallocate(c); mm.deallocate(a);