Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
General <strong>Java</strong> Questions I<br />
public interface Iterator {<br />
boolean hasNext();<br />
Object next();<br />
void remove(); // Optional<br />
}<br />
The hasNext method is identical in function to Enumeration.hasMoreElements, and<br />
the next method is identical in function to Enumeration.nextElement. The remove<br />
method removes from the underlying Collection the last element that was returned by<br />
next. The remove method may be called only once per call to next, and throws an<br />
exception if this condition is violated. Note that Iterator.remove is the only safe way to<br />
modify a collection during iteration; the behavior is unspecified if the underlying<br />
collection is modified in any other way while the iteration is in progress.<br />
The following snippet shows you how to use an Iterator to filter a Collection, that is, to<br />
traverse the collection, removing every element that does not satisfy some condition:<br />
static void filter(Collection c) {<br />
for (Iterator i = c.iterator(); i.hasNext(); )<br />
if (!cond(i.next()))<br />
i.remove();<br />
}<br />
Two things should be kept in mind when looking at this simple piece of code:<br />
The code is polymorphic: it works for any Collection that supports element removal,<br />
regardless of implementation. That's how easy it is to write a polymorphic algorithm<br />
under the collections framework!<br />
It would have been impossible to write this using Enumeration instead of Iterator,<br />
because there's no safe way to remove an element from a collection while traversing<br />
it with an Enumeration.<br />
How can I find the first dimension length of the 2-dimenstions array? I have use<br />
the array[].length but it does not work, how can I solve this problem?<br />
Answer: <strong>Java</strong> doesn't really have "multidimensional arrays", only arrays of arrays. So<br />
try: array[0].length and you will get this dimension.<br />
I guess what I'm asking is "Is java.util.Hashtable thread safe?"<br />
Q: It's been a while since I've used hashtables for anything significant, but I seem to<br />
recall the get() and put() methods being synchronized.<br />
The <strong>Java</strong>Docs don't reflect this. They simply say that the class Hashtable is<br />
synchronized. What can I assume? If several threads access the hashtable at the<br />
same time (assuming they are not modifying the same entry), the operations will<br />
succeed, right? I guess what I'm asking is "Is java.util.Hashtable thread safe?"<br />
Answer: That is right! It is recommendable, if you have questions like these, always<br />
look at source for the API, it's freely available.<br />
Q: I try to copy an object of my own using the clone() method from<br />
java.lang.Object, but this is a protected method so I can't use it. Is there some other<br />
way to get my objective of duplicating an arbitrary object?<br />
Answer: If you want to clone your object, you need to make it cloneable. To achieve<br />
this, you need to do two things:<br />
1. implement the interface Cloneable<br />
file:///F|/350_t/350_tips/general_java-I.htm (12 of 31) [2002-02-27 21:18:17]