Appendix 10 Cloning
A clone of an object is an exact copy of the object. The emphasis here is on both exact and copy. A clone should have exactly the same data values as the original object, and it should be a distinct object and not simply another name for the original one.
A clone is made by invoking the method named clone
. Recall from Chapter 8 that this method is defined in the class Object
but needs to be overridden— that is, redefined before it will behave correctly for a specific class.
The standard class ArrayList
is a class that defines its own clone
method, as Chapter 12 mentioned. Suppose we define a list of strings using ArrayList
, as follows:
ArrayList<String> aList = new ArrayList<String>();
<Some code to fill aList>
We then can ...
Get Java: An Introduction to Problem Solving and Programming, 8th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.