CATEGORII DOCUMENTE |
Asp | Autocad | C | Dot net | Excel | Fox pro | Html | Java |
Linux | Mathcad | Photoshop | Php | Sql | Visual studio | Windows | Xml |
In any collection class, you must have a way to put things in and a way to get things out. After all, that's the primary job of a collection - to hold things. In the Vector, addElement( ) is the way that you insert objects, and elementAt( ) is one way to get things out. Vector is quite flexible - you can select anything at any time, and select multiple elements at once using different indexes.
If you want to start thinking at a higher level, there's a drawback: you need to know the exact type of the collection in order to use it. This might not seem bad at first, but what if you start out using a Vector, and later on in your program you decide, for efficiency, that you want to change to a List (which is part of the Java 1.2 collections library)? Or you'd like to write a piece of code that doesn't know or care what type of collection it's working with.
The concept of an iterator can be used to achieve this next level of abstraction. This is an object whose job is to move through a sequence of objects and select each object in that sequence without the client programmer knowing or caring about the underlying structure of that sequence. In addition, an iterator is usually what's called a "light-weight" object; that is, one that's cheap to create. For that reason, you'll often find seemingly strange constraints for iterators; for example, some iterators can move in only one direction.
The Java Enumeration is an example of an iterator with these kinds of constraints. There's not much you can do with one except:
Ask a collection to hand you an Enumeration using a method called elements( ). This Enumeration will be ready to return the first element in the sequence on your first call to its nextElement( ) method.
Get the next object in the sequence with nextElement( ).
See if there are any more objects in the sequence with hasMoreElements( ).
That's all. It's a simple implementation of an iterator, but still powerful. To see how it works, let's revisit the CatsAndDogs.java program from earlier in the chapter. In the original version, the method elementAt( ) was used to select each element, but in the following modified version an enumeration is used:
//: CatsAndDogs2.java
// Simple collection with Enumeration
import java.util.*;
class Cat2
void print()
}
class Dog2
void print()
}
public class CatsAndDogs2
} ///:~
You can see that the only change is in the last few lines. Instead of:
for(int i = 0; i < cats.size(); i++)
((Cat)cats.elementAt(i)).print();
an Enumeration is used to step through the sequence:
while(e.hasMoreElements())
((Cat2)e.nextElement()).print();
With the Enumeration, you don't need to worry about the number of elements in the collection. That's taken care of for you by hasMoreElements( ) and nextElement( ).
As another example, consider the creation of a general-purpose printing method:
//: HamsterMaze.java
// Using an Enumeration
import java.util.*;
class Hamster
public String toString()
}
class Printer
}
public class HamsterMaze
} ///:~
Look closely at the printing method:
static void printAll(Enumeration e)
Note that there's no information about the type of sequence. All you have is an Enumeration, and that's all you need to know about the sequence: that you can get the next object, and that you can know when you're at the end. This idea of taking a collection of objects and passing through it to perform an operation on each one is powerful and will be seen throughout this book.
This particular example is even more generic, since it uses the ubiquitous toString( ) method (ubiquitous only because it's part of the Object class). An alternative way to call print (although probably slightly less efficient, if you could even notice the difference) is:
System.out.println('' + e.nextElement());
which uses the "automatic conversion to String" that's wired into Java. When the compiler sees a String, followed by a '+', it expects another String to follow and calls toString( ) automatically. (In Java 1.1, the first String is unnecessary; any object will be converted to a String.) You can also perform a cast, which has the effect of calling toString( ):
System.out.println((String)e.nextElement());
In general, however, you'll want to do something more than call Object methods, so you'll run up against the type-casting issue again. You must assume you've gotten an Enumeration to a sequence of the particular type you're interested in, and cast the resulting objects to that type (getting a run-time exception if you're wrong).
Politica de confidentialitate | Termeni si conditii de utilizare |
Vizualizari: 531
Importanta:
Termeni si conditii de utilizare | Contact
© SCRIGROUP 2024 . All rights reserved