General Java Questions I
iteration with well defined semantics. 
Method names have been improved. 
The first point is important: There was no safe way to remove elements from a
collection while traversing it with an Enumeration. The semantics of this operation
were ill defined, and differed from implementation to implementation. 
The Iterator interface is shown below: 
public interface Iterator {
   boolean hasNext();
   Object next();
   void remove(); // Optional
}
The hasNext method is identical in function to Enumeration.hasMoreElements, and
the next method is identical in function to Enumeration.nextElement. The remove
method removes from the underlying Collection the last element that was returned by
next. The remove method may be called only once per call to next, and throws an
exception if this condition is violated. Note that Iterator.remove is the only safe way to
modify a collection during iteration; the behavior is unspecified if the underlying
collection is modified in any other way while the iteration is in progress. 
The following snippet shows you how to use an Iterator to filter a Collection, that is, to
traverse the collection, removing every element that does not satisfy some condition: 
static void filter(Collection c) {
   for (Iterator i = c.iterator(); i.hasNext(); )
       if (!cond(i.next()))
       i.remove();
}
Two things should be kept in mind when looking at this simple piece of code: 
The code is polymorphic: it works for any Collection that supports element removal,
regardless of implementation. That s how easy it is to write a polymorphic algorithm
under the collections framework! 
It would have been impossible to write this using Enumeration instead of Iterator,
because there s no safe way to remove an element from a collection while traversing
it with an Enumeration.
How can I find the first dimension length of the 2 dimenstions array? I have use
the array[].length but it does not work, how can I solve this problem? 
Answer: Java doesn t really have "multidimensional arrays", only arrays of arrays. So
try: array[0].length and you will get this dimension.
I guess what I m asking is "Is java.util.Hashtable thread safe?"
Q: It s been a while since I ve used hashtables for anything significant, but I seem to
recall the get() and put() methods being synchronized. 
The JavaDocs don t reflect this. They simply say that the class Hashtable is
synchronized. What can I assume? If several threads access the hashtable at the
same time (assuming they are not modifying the same entry), the operations will
succeed, right? I guess what I m asking is "Is java.util.Hashtable thread safe?"
Answer: That is right! It is recommendable, if you have questions like these, always
look at source for the API, it s freely available.
file:///F|/a_jsite/350_tips/general_java I.htm (13 of 33) [2001 07 08 11:24:51]






footer




 

 

 

 

 Home | About Us | Network | Services | Support | FAQ | Control Panel | Order Online | Sitemap | Contact

java web hosting

 

Visionwebhosting.net Business web hosting division of Web Design Plus. All rights reserved