Java Reference
In-Depth Information
Programming Tip
Even though you might have written a correct definition of a method, do not hesitate to revise
it if you think of a better implementation.
2.30
Te s t i n g . Our class ArrayBag is essentially complete. We can use the previously tested methods—which
we assume are correct—in the tests for remove and clear . Starting with a bag that is not full, the online
program ArrayBagDemo3 removes the bag's entries until it is empty. It also includes similar tests begin-
ning with a full bag. Finally, we should consolidate our previous tests and run them again. The source
code available on the topic's website identifies our test program as ArrayBagDemo and the complete ver-
sion of the class as ArrayBag .
Using Array Resizing to Implement the ADT Bag
2.31
An array has a fixed size, which is chosen by either the programmer or the user before the array is
created. A fixed-size array is like a classroom. If the room contains 40 desks but only 30 students, we
waste 10 desks. If 40 students are taking the course, the room is full and cannot accommodate any-
one else. Likewise, if we do not use all of the locations in an array, we waste memory. If we need
more, we are out of luck.
Using a fixed-size array to implement the ADT bag, therefore, limits the size of the bag. When
the array, and hence the bag, becomes full, the method isFull returns true and subsequent calls to
the add method return false. Some applications can use a bag or other collection that has a limited
capacity. For other applications, however, we need the size of a collection to grow without bound.
We will now show you how a group of items can be as large as you want—within the limits of your
computer's memory—but still be in an array.
Resizing an Array
2.32
The strategy. When a classroom is full, one way to accommodate additional students is to move to
a larger room. In a similar manner, when an array becomes full, you can move its contents to a
larger array. This process is called resizing an array. Figure 2-8 shows two arrays: an original array
of five consecutive memory locations and another array—twice the size of the original array—that
is in another part of the computer's memory. If you copy the data from the original smaller array to
the beginning of the new larger array, the result will be like expanding the original array. The only
glitch in this scheme is the name of the new array: You want it to be the same as the name of the old
array. You will see how to accomplish this momentarily.
FIGURE 2-8
Resizing an array copies its contents to a larger second array
Original array
Larger array
2.33
The details. Suppose we have an array that myArray references, as Figure 2-9a illustrates. We first
define an alias oldArray that also references the array, as Figure 2-9b shows. The next step is to cre-
ate a new array that is larger than the original array and let myArray reference this new array. As pic-
tured in Figure 2-9c, the new array typically doubles the size of the original array. The final step
 
 
 
Search WWH ::




Custom Search