Java Reference
In-Depth Information
Readers with very little programming experience may be better off skipping this entire
section until they become more comfortable with Java. Other readers may prefer to read
only the first subsection and possibly the immediately following Pitfall subsection.
Simple Copy Constructors and
clone
Methods
★
Display 15.12 contains a copy constructor and
clone
method definitions that could
be added to the definition of the generic linked list class in Display 15.8. The real
work is done by the private helping method
copyOf
, so our discussion focuses on the
method
copyOf
.
The private method
copyOf
takes an argument that is a reference to the head node
of a linked list and returns a reference to the head node of a copy of that linked list.
The easiest way to do this would be to simply return the argument. This would, how-
ever, simply produce another name for the argument list. We do not want another
name; we want another list. So, the method goes down the argument list one node at a
time (with
position
) and makes a copy of each node. The linked list of the calling
object is built up node by node by adding these new nodes to its linked list. However,
there is a complication. We cannot simply add the new nodes at the head (start) end of
the list being built. If we did, then the nodes would end up in the reverse of the
desired order. So, the new nodes are added to the end of the linked list being built.
The variable
end
of type
Node<T>
is kept positioned at the last node so that it is possi-
ble to add nodes at the end of the linked list being built. In this way a copy of the list
in the calling object is created so that the order of the nodes is preserved.
The copy constructor is defined by using the private helping method
copyOf
to cre-
ate a copy of the list of nodes. Other details of the copy constructor and the
clone
method are done in the standard way.
Although the copy constructor and the
clone
method each produce a new linked
list with all new nodes, the new list is not truly independent because the data objects
are not cloned. See the next Pitfall section for a discussion of this point. One way to fix
this shortcoming is discussed in the Programming Tip subsection entitled “Use a Type
Parameter Bound for a Better
clone
.”
Exceptions
★
A generic data structure, such as the class
LinkedList
in Display 15.12, is likely to have
methods that throw exceptions. Situations such as a
null
argument to the copy con-
structor might be handled differently in different situations, so it is best to throw a
NullPointerException
if this happens and let the programmer who is using the linked
list handle the exception. This is what we did with the copy constructor in Display
15.12. A
NullPointerException
is an unchecked exception, which means that it need
not be caught or declared in a
throws
clause. When thrown by a method of a linked list
class, it can be treated simply as a run-time error message. The exception can instead be
caught in a
catch
block if there is some suitable action that can be taken.