[ Pobierz całość w formacie PDF ]
.[ Add Comment ]In addition, the minimum size of theBitSet is that of a long: 64 bits.This implies that ifyou’re storing anything smaller, like 8 bits, a BitSet will bewasteful; you’re better off creating your own class, or just an array, tohold your flags if size is an issue.[ Add Comment ]A normal container expands as you addmore elements, and the BitSet does this as well.The following exampleshows how the BitSet works://: c09:Bits.java// Demonstration of BitSet.import java.util.*;public class Bits {static void printBitSet(BitSet b) {System.out.println("bits: " + b);String bbits = new String();for(int j = 0; j < b.size() ; j++)bbits += (b.get(j) ? "1" : "0");System.out.println("bit pattern: " + bbits);}public static void main(String[] args) {Random rand = new Random();// Take the LSB of nextInt():byte bt = (byte)rand.nextInt();BitSet bb = new BitSet();for(int i = 7; i >=0; i--)if(((1 << i) & bt) != 0)bb.set(i);elsebb.clear(i);System.out.println("byte value: " + bt);printBitSet(bb);short st = (short)rand.nextInt();BitSet bs = new BitSet();for(int i = 15; i >=0; i--)if(((1 << i) & st) != 0)bs.set(i);elsebs.clear(i);System.out.println("short value: " + st);printBitSet(bs);int it = rand.nextInt();BitSet bi = new BitSet();for(int i = 31; i >=0; i--)if(((1 << i) & it) != 0)bi.set(i);elsebi.clear(i);System.out.println("int value: " + it);printBitSet(bi);// Test bitsets >= 64 bits:BitSet b127 = new BitSet();b127.set(127);System.out.println("set bit 127: " + b127);BitSet b255 = new BitSet(65);b255.set(255);System.out.println("set bit 255: " + b255);BitSet b1023 = new BitSet(512);b1023.set(1023);b1023.set(1024);System.out.println("set bit 1023: " + b1023);}} ///:~The random number generator is used tocreate a random byte, short, and int, and each one istransformed into a corresponding bit pattern in a BitSet.This works finebecause a BitSet is 64 bits, so none of these cause it to increase insize.Then a BitSet of 512 bits is created.The constructor allocatesstorage for twice that number of bits.However, you can still set bit 1024 orgreater.[ Add Comment ]SummaryTo review the containers provided in thestandard Java library:[ Add Comment ]An array associatesnumerical indices to objects.It holds objects of a known type so that youdon’t have to cast the result when you’re looking up an object.Itcan be multidimensional, and it can hold primitives.However, its size cannot bechanged once you create it.[ Add Comment ]ACollection holds single elements, while a Map holds associatedpairs.[ Add Comment ]Likean array, a List also associates numerical indices to objects—youcan think of arrays and Lists as ordered containers.The Listautomatically resizes itself as you add more elements.But a List canhold only Object references, so it won’t hold primitives and youmust always cast the result when you pull an Object reference out of acontainer.[ Add Comment ]Usean ArrayList if you’re doing a lot of random accesses, and aLinkedList if you will be doing a lot of insertions and removals in themiddle of the list.[ Add Comment ]Thebehavior of queues, deques, and stacks is provided via the LinkedList.[ Add Comment ]AMap is a way to associate not numbers, but objects with otherobjects.The design of a HashMap is focused on rapid access, while aTreeMap keeps its keys in sorted order, and thus is not as fast as aHashMap.[ Add Comment ]ASet only accepts one of each type of object.HashSets providemaximally fast lookups, while TreeSets keep the elements in sorted order.[ Add Comment ]There’sno need to use the legacy classes Vector, Hashtable andStack in new code.[ Add Comment ]Thecontainers are tools that you can use on a day-to-day basis to make yourprograms simpler, more powerful, and more effective.[ Add Comment ]ExercisesSolutions to selected exercisescan be found in the electronic document The Thinking in Java AnnotatedSolution Guide, available for a small fee fromwww.BruceEckel.com.Create an array ofdouble and fill( ) it using RandDoubleGenerator.Printthe results.[ Add Comment ]Createa new class called Gerbil with an int gerbilNumber that’sinitialized in the constructor (similar to the Mouse example in thischapter).Give it a method called hop( ) that prints out whichgerbil number this is, and that it’s hopping.Create an ArrayListand add a bunch of Gerbil objects to the List.Now use theget( ) method to move through the List and callhop( ) for each Gerbil
[ Pobierz całość w formacie PDF ]