General Java Questions II
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.
Q: I try to copy an object of my own using the clone() method from
java.lang.Object, but this is a protected method so I can t use it. Is there some other
way to get my objective of duplicating an arbitrary object?
Answer: If you want to clone your object, you need to make it cloneable. To achieve
this, you need to do two things:
1. implement the interface Cloneable
2. override the method clone(), so that it
a. becomes public
b. calls super.clone()
c. if necessary, clones any members, or
d. if a member can t be cloned, creates a new instance.
Simple example:
public MyClass implements Cloneable {
int someNumber;
String someString;
public Object clone() {
// primitives and Strings are no
// problem
return super.clone();
}
}
In this case the method clone() of the class MyClass returns a new instance of
file:///F|/a_jsite/350_tips/general_java II.htm (11 of 14) [2001 07 08 11:24:53]
footer
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved