Java Reference
In-Depth Information
LISTING 8.4
Continued
24: if (!list.contains(code)) {
25: list.add(code);
26: }
27: }
28:
29: public static void main(String[] arguments) {
30: CodeKeeper2 keeper = new CodeKeeper2(arguments);
31: }
32: }
8
The only modifications to the class are in line 4, where the new generics declaration for a
vector of strings is made, and lines 18-19, the simpler for loop.
NOTE
Generics can limit data structures to any kind of class, even ones
of your own design. For example, a checkbook application could
define a Check class for individual financial checks, holding each
in a Vector defined to hold only objects of that class.
Summary
Today, you learned about several data structures you can use in your Java programs:
Bit sets —Large sets of Boolean on-or-off values
n
Stacks —Structures in which the last item added is the first item removed
n
Vectors —Arrays that can change in size dynamically and be shrunken or expanded
as needed
n
Hash tables —Objects stored and retrieved using unique keys
n
These data structures are part of the java.util package, a collection of useful classes for
handling data, dates, strings, and other things. The addition of generics and new for
loops for iteration enhances their capabilities.
Learning about the ways you can organize data in Java has benefits in all aspects of soft-
ware development. Whether you're learning the language to write servlets, console pro-
grams, consumer software with a graphical user interface, or something else entirely, you
will need to represent data in numerous ways.
 
 
Search WWH ::




Custom Search