Java Reference
In-Depth Information
is overshadowed by the time it takes to locate the desired entry. We will talk more about such a search
later in this topic.
Using a fixed-size array limits the capacity of a bag, which is usually a disadvantage. Resizing
an array dynamically enables you to increase the array's size but requires copying data. You should
realize that the array entries that we copy are references, and so do not occupy much space nor take
much time to move. Some languages other than Java store the data itself within the array. In such
cases, moving large, complex objects can be quite time-consuming.
Note: When you use an array to implement the ADT bag,
Adding an entry to the bag is fast
Removing an unspecified entry is fast
Removing a particular entry requires time to locate the entry
Increasing the size of the array requires time to copy its entries
C HAPTER S UMMARY
You can use a Java array to define a relatively simple implementation of the ADT bag, but other implementations
are possible.
Adding an entry right after the last entry in an array does not disturb the position of existing entries. Likewise,
deleting the last entry from an array does not disturb the position of existing entries.
Because a bag does not maintain its entries in a specific order, deleting an entry does not require you to
move all subsequent array entries to the next lower position. Instead, you can replace the entry that you want
to delete with the last entry in the array and replace the last entry with null .
Identifying and implementing a class's central, or core, methods before any others is a good strategy to use
when you expect the class to be lengthy or complex. Use stubs for the remaining methods.
Test a class at each stage of its development, particularly after adding a significant method.
Using a fixed-size array can result in a full bag.
Resizing an array makes it appear to change size. To do so, you allocate a new array, copy the entries from the
original array to the new array, and use the original variable to reference the new array.
Resizing an array enables you to implement collections whose contents are limited in number only by the
size of the computer's memory.
P ROGRAMMING T IPS
When defining a class, implement and test a group of core methods. Begin with methods that add to a collec-
tion of objects and/or have involved implementations.
A class should not return a reference to an array that is a private data field.
Do not wait until you complete the implementation of an ADT before testing it. By writing stubs, which are
incomplete definitions of required methods, you can begin testing early in the process.
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.
 
 
Search WWH ::




Custom Search