Java Reference
In-Depth Information
The next() method always returns an object of the class Object . You can cast this to
another class that the structure holds, as in this example for a data structure that holds
String objects:
while (users.hasNext()) {
String ob = (String) users.next();
System.out.println(ob);
8
}
Because Iterator is an interface, you'll never use it directly as a
data structure. Instead, you'll use the methods defined by
Iterator for structures that implement the interface. This architec-
ture provides a consistent interface for many of the standard data
structures, which makes them easier to learn and use.
NOTE
Bit Sets
The BitSet class is useful when you need to represent a large amount of binary data, bit
values that can be equal only to 0 or 1. These also are called on-or-off values (with 1 rep-
resenting on and 0 representing off) or Boolean values (with 1 representing true and 0
representing false).
With the BitSet class, you can use individual bits to store Boolean values without
requiring bitwise operations to extract bit values. You simply refer to each bit using an
index. Another nice feature is that it automatically grows to represent the number of
bits required by a program. Figure 8.1 shows the logical organization of a bit set data
structure.
FIGURE 8.1
The organization of
a bit set.
Index
0
1
2
3
Value
Boolean0
Boolean1
Boolean2
Boolean3
You can use a BitSet object to hold attributes that can easily be modeled by Boolean
values. Because the individual bits in a set are accessed via an index, you can define each
attribute as a constant index value, as in this class:
class ConnectionAttributes {
public static final int READABLE = 0;
public static final int WRITABLE = 1;
public static final int STREAMABLE = 2;
public static final int FLEXIBLE = 3;
}
Search WWH ::




Custom Search