Java Reference
In-Depth Information
public
T getEntry(
int
givenPosition)
{
T result =
null
;
// return value
if
((givenPosition >= 1) && (givenPosition <= list.size()))
{
assert
!isEmpty();
result = list.get(givenPosition - 1);
}
// end if
return
result;
}
// end getEntry
13.23
Remaining methods.
The method
clear
uses the statement
list.clear();
to remove all entries from the vector and hence from the list. Likewise, the implementations of the
methods
contains
,
getLength
, and
isEmpty
are simple and left as exercises.
Note:
The methods in our class
VectorList
function similarly to methods in Java's class
Vector
, but their specifications differ. In implementing
ListInterface
,
VectorList
simply
invokes methods of the class
Vector
.
VectorList
is an example of an adapter class, which
we described in Segment C.3 of Appendix C.
Note:
Since Java's class
Vector
uses an array in its implementation, the two implementa-
tions of the ADT list that we have covered so far are array based. Both implementations use a
resizable array to contain the list's entries, and so a list can grow in size as needed.
Note:
Writing
VectorList
is certainly easier than writing the array-based implementa-
tion that this chapter describes. Since the methods of
VectorList
invoke the methods of
Vector
, they can require more execution time than those of
AList
. However, this time
increase typically is insignificant.
Note:
When you use an array or a vector to implement the ADT list,
•
Retrieving the entry at a given position is fast
•
Adding an entry at the end of a list is fast
•
Adding or removing an entry that is between other entries requires shifting them within
the array
•
Increasing the size of the array or vector requires copying entries
C
HAPTER
S
UMMARY
•
The two implementations of the ADT list in this chapter use an array to store the items in a list.
•
Using an array results in a straightforward implementation of the list, but it is somewhat more involved than
the implementations of either the ADT bag or the ADT stack.
•
An array provides direct access to any of its elements, so a method such as
getEntry
has a simple,
efficient implementation.