Java Reference
In-Depth Information
Using ArrayList Methods
Both of the set() and validate() instance methods call methods of the
ArrayList object created in the constructors. Earlier in the chapter, a brief
rationale was presented as to why an ArrayList was an appropriate collection to
use for the password history list. Although a LinkedList could be used to imple-
ment the required functionality for this Password class, an ArrayList provides
more efficient access, because many of its operations take a constant time regard-
less of the size of the list; this access time is low compared to that for a LinkedList.
Because the list will have a known size, but a size that might be changed, the
resizeable array provided by the ArrayList class is an appropriate solution. As dis-
cussed earlier, the ArrayList class has a known size or capacity, which expands
automatically as items are added beyond the initial capacity. It does not reduce
automatically, however, even if members are removed. The capacity can be explic-
itly reduced, as was done earlier by calling the trimToSize() method; however, it
never should be reduced to less than the current number of elements.
Another important issue when considering the appropriate collection to use
for the password history list is if the collection supports all required functional-
ity with relative ease of use for the programmer. The ArrayList object provides
several methods — including those called by the set() and validate() methods
coded in the previous steps — that provide the required functionality for the
password history list. Table 9-5 lists the ArrayList methods used in the Password
class created in this chapter.
Table 9-5
ArrayList Methods Used in the Password Class
METHOD
DESCRIPTION
boolean add(Object o)
Appends the element, which can be any object type, to the end of the list. The
method returns true because a List collection typically allows duplicate elements.
boolean contains(Object o)
Returns true if the list contains the specified object as an element.
Object get(int index)
Returns the element in the list at the position specified by index.
boolean isEmpty()
Returns true if the list has no elements; otherwise, returns false.
Object remove(int index)
Removes the element at the specified index.
int size()
Returns the number of elements in the list.
trimToSize()
Trims the capacity of the list to the list's current size.
As defined in the requirements document, the Password class is required to
keep a password history list of up to 10 old password values. The reason for main-
taining a password history list is to prevent reuse of a recent password value when
the user changes the current password value. The password history list should,
therefore, provide a means to determine whether the new password value entered
by the user already exists in the list. The contains() method in line 144 in
Figure 9-31 on page 570 provides this capability, returning true if the password
history list already contains the new password value.
 
Search WWH ::




Custom Search