February 2017
Beginner
1056 pages
28h 57m
English
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 ...
Read now
Unlock full access