Java Reference
In-Depth Information
The size() method (line 146 of Figure 9-31 on page 570) returns the current
number of elements in the ArrayList and, therefore, can be used to determine if
the maximum password history list size has been reached. As with most kinds of
arrays and lists in Java, the numbering of elements begins with zero. Once the
password history list has reached its maximum size, an additional password
value is added only after the oldest value is removed by using the remove()
method in line 147 to remove the element at index zero. In this way, the pass-
word history list is maintained within the maximum size limit.
As indicated in the requirements document, new passwords cannot match
any contained in the active password history list. Previously used passwords are
allowed for reuse, but only when they no longer are in the password history list.
The password history list thus should store passwords in the order added; this
ensures that the older a password value is, the sooner it will be available for
reuse. The ArrayList add() method in line 149 adds a new object to the end of
the list. Adding the object to the end of the list maintains items in the order they
were added, with the oldest having an index value of zero and the newest having
an index value of one less than the size of the list. The Collection interface speci-
fies that the add() method returns true if the element is added and false if it is
rejected as a duplicate. As discussed earlier, the ArrayList implementation of the
add() method returns true. If the element is not added for any other reason, an
exception is thrown.
As shown in line 174 of Figure 9-32 on page 570, the validate() method calls
the ArrayList isEmpty() method , allowing for a quick determination of whether
any items exist in the password history list or not. If the list is not empty, the
size() method is called in line 176 to obtain the current size of the password his-
tory list. The result of this call can be used to obtain the index of the latest item
added, which is one less than the current size of the password history list. This
index then is used with the get() method to access the indexed ArrayList item
directly (the current password value, in this case), regardless of the size of the list.
The get() method in line 177 of Figure 9-32 returns a reference to an ele-
ment of type Object, even though the passwords in the list are String objects. All
objects in Java inherit Object as the root parent class, so any object may be refer-
enced most generally as an instance of Object, regardless of its data type. This is
how an ArrayList can maintain a list of any type of object. Recall from Chapter 3
that the cast operation converts from one data type to another, by entering the
new data type in parentheses before a literal or variable. Referencing an object as
an instance of a more general parent class is called upcasting and can be done
implicitly, that is, without using the cast operator. When a programmer is ready
to retrieve data from an ArrayList, the cast operator must be used to reference an
object as an instance of a more specific class. Casting from a more general type
J ava 2
v 5 . 0
J ava 2
JAVA UPDATE
v 5 . 0
Generics
The Java Collections
Framework now uses
generics, also known as
parameterized data
types, so the program
may specify the exact
type of object a given
collection can store,
rather than treating all
elements as Objects.
This removes the
requirement to cast
down a retrieved object
to a valid type. It also
prevents throwing an
exception due to
miscasting a retrieved
object. Because the
data type of objects
stored in the collection
is specified, an attempt
to store a different type
of object will generate
a compile-time error.
Validating the data type
at compile time is said
to make the collection
typesafe.
To specify a data type
for use with a collec-
tion, place the data
type within angle
brackets, < >, after the
collection name. Only
reference types, not
primitives, can be used
with generics, although
autoboxing may make
it appear otherwise.
23
private ArrayList< String > pswdHistory;
If you want to modify
the Password class to
make its use of the
ArrayList collection
typesafe, replace lines
23, 29, 43, 50, and
177, as shown in the
following code.
29 pswdHistory = new ArrayList< String > ( maxHistory ) ;
43
pswdHistory = new ArrayList< String > ( maxHistory ) ;
50 pswdHistory = new ArrayList< String > ( maxHistory ) ;
177 currentPswd = pswdHistory.get ( currentPswdIndex ) ;
 
Search WWH ::




Custom Search