To practice with the process of cloning we present the case as shown in the slides and the theory:
Create an ArrayList
with some Persons in it.
Pass that list to a method that removes the first element and changes the third one.
private void changeList(ArrayList<Person> list) {
// ...
}
Confirm that your original list has now been changed.
Before passing your list to the changeList
method, first create a new list and copy all Persons from the original to the copy of the list.
Confirm that in your original list the first Person
is still present, but the third one is still changed.
This time not only create a new copy the list, but also create copies of all Person
s.
Confirm that in your original list the first Person
is still present. This time the third Person
remains unchanged. All changes are now restricted to the cloned list.
After creating a clone of your list, you may want to actually test that you succeeded.
The test method assertEquals
uses the equals
method to confirm that two object instances have equivalent values.
Please use the assertNotSame
(this is the inverse of assertSame
) which confirms that although they are equal
they are not the very same reference!
So each element in the original list should be equal to each element in the clone, but they must not be the same object.