Java Reference
In-Depth Information
TIP: Cloning Is an “All or Nothing” Affair
If you define a clone method, then you should do so following the official Java
guidelines, as we did in Display 15.14. In particular, you should always have the class
implement the Cloneable interface. If you define a clone method in any other way,
you may encounter problems in some situations. If you want to have a method for
producing copies of objects but do not want to follow the official guidelines on how
to define a clone method, then use some other name for your “ clone -like” method,
such as copier , or make do with just a copy constructor.
Self-Test Exercises
9. In the defi nition of copyOf in Display 15.14 , can we replace
newHead =
new Node<T>((T)(position.data).clone( ), null );
with the following, which uses the copy constructor of T instead of the clone
method of T ?
newHead =
new Node<T>( new T(position.data), null );
10. The defi nition of the clone method in Display 15.14 returns a value
of type LinkedList<T> . But the class being defi ned implements the
PubliclyCloneable interface, and that interface says the value returned must
be of type Object . Is something wrong?
15.3
Iterators
Play it again, Sam.
Attributed (incorrectly) to the movie Casablanca , which contains similar lines. 2
When you have a collection of objects, such as the nodes of a linked list, you often need
to step through all the objects in the collection one at a time and perform some action
on each object, such as writing it out to the screen or in some way editing the data in
each object. An iterator is any object that allows you to step through the list in this way.
iterator
2 There is a Woody Allen movie with this title, but it is based on the misquote from Casablanca , which
was in common use before the movie came out.
 
 
Search WWH ::




Custom Search