Photo Rating Website
Home Maximum R The Cambr 0877 Ch09 Niewolnica

[ Pobierz całość w formacie PDF ]

operations with an original bit set of {1, 2, 3, 4, 5} and a passed in set of {1, 3, 5, 7}.
Table 6-2: Set Manipulation
OPERATION RESULTS
and() {1, 3, 5}
or() {1, 2, 3, 4, 5, 7}
xor() {2, 4, 7}
andNot() {2, 4}
75
Determining Set Size
If there is a size difference between the two sets, the size of the resulting set depends on the method called.
Under no condition will the contents of the passed-in set change in size or content as a result of the method
call.
" and(): If the size of the current set is smaller than the passed-in set, the extra bits are ignored. If the
size of the current set is larger than the passed-in set, the extra bits are cleared/set to zero.
" or() / xor(): If the size of the current set is smaller than the passed-in set, the size of the current set is
increased to match the size of the passed-in set. All new bits are cleared/set to zero before performing
the operation.
" andNot(): If the sizes are different in either direction, the extra bits are ignored/unchanged.
Note A NullPointerException will be thrown if you pass in a null set to any of these methods.
Determining Set Size
Think of a BitSet as a dynamically growing array composed of bits, similar to a vector. This dynamically
growing structure has two values describing its internal dimensions: a size and a length.
public int size()
public int length()
The size() method will return the number of bits reserved in the set and grows in increments of sixty-four.
Think of this value like the capacity when working with a vector. On the other hand, the length() of a bit set is
the last position that is set. Since positions are indexed starting from zero, this equates to the index of the
highest set bit plus one.
Note One odd behavior of a bit set's length is that if you clear the highest set bit, the set's length can drop
more than one depending upon where the next highest set bit is located.
Cloning Bit Sets
The BitSet class implements the Cloneable interface, providing you with a clone() method.
public Object clone()
When you clone() a BitSet, you create another set with the same size and the same bit positions set.
Checking Bit Sets for Equality
The BitSet class overrides the equals() method from Object to define the equality of bit sets.
public boolean equals(Object object)
Two bit sets are defined as equal if both bit sets have the same set or clear state at every position. In other
words, this[index]=object[index] must be true for each index in both sets. When one set is shorter than the
other, the remaining elements would need to be clear in the longer set for both sets to be equal.
Hashing Bit Sets
Besides overriding the equals() method of Object, the BitSet class also overrides the hashCode() method. The
generated hash code is only based on the position of the set bits:
76
Using BitSet: an Example
public int hashCode()
Using BitSet: an Example
To demonstrate the usage of the BitSet class, Listing 6-1 creates a set of candy where the bit is set if the name
has an even number of characters. It then prints out all the odd types of candy and shows the set size and
length. Finally, it demonstrates the andNot() method by combining the first set with a second set where the
first four elements are all set.
Listing 6-1: Demonstrating the use of BitSet.
import java.util.BitSet;
public class BitOHoney {
public static void main (String args[]) {
String names[] = {
"Hershey's Kisses", "Nestle's Crunch",
"Snickers", "3 Musketeers",
"Milky Way", "Twix", "Mr. Goodbar",
"Crunchie", "Godiva", "Charleston Chew",
"Cadbury's", "Lindt", "Aero", "Hebert",
"Toblerone", "Smarties", "LifeSavers",
"Riesen", "Goobers", "Raisinettes", "Nerds",
"Tootsie Roll", "Sweet Tarts", "Cotton Candy"};
BitSet bits = new BitSet();
for (int i=0, n=names.length; i
if ((names[i].length() % 2) == 0) {
bits.set(i);
}
}
System.out.println(bits);
System.out.println("Size : " + bits.size());
System.out.println("Length: " + bits.length());
for (int i=0, n=names.length; i
if (!bits.get(i)) {
System.out.println(names[i] + " is odd");
}
}
BitSet bites = new BitSet();
bites.set(0);
bites.set(1);
bites.set(2);
bites.set(3);
bites.andNot(bits);
System.out.println(bites);
}
} [ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • spartaparszowice.keep.pl
  • Naprawdę poczułam, że znalazłam swoje miejsce na ziemi.

    Designed By Royalty-Free.Org