1

I try to get my head straight about cloning in Java with my own ArrayList.

For what I see this code

@Override
public Object clone() {
    try {
        MyArrayList<E> v = (MyArrayList<E>) super.clone();
        v.elementData = Arrays.copyOf(elementData, size);
        return v;
    } catch (CloneNotSupportedException e) {
        throw new InternalError();
    }
}

Gives the same result as

@Override
public Object clone() {
    try {
        return (MyArrayList<E>) super.clone();
    } catch (CloneNotSupportedException e) {
        throw new InternalError();
    }
}

Am I doing something wrong or why use the Arrays.copyOf()?

3
  • What is elementData in relation to MyArrayList ? Are you trying to do a "deep copy" ? Commented Feb 7, 2015 at 22:22
  • I must confess that I don't understand the difference between a "deep" and "shallow" copy. elementData is the element data in the ArrayList. The first code is taken from the ArrayList source code. Why the Arrays.copyOf(), for me the second code example gives the same result; meaning both copys gets all data. Commented Feb 7, 2015 at 22:28
  • 1
    Try to modify some elements in the array of the first list, and then check if the elements in the array of the second list reflect that ;) then you'll understand. Commented Feb 7, 2015 at 22:35

1 Answer 1

3

No it doesn't, the first method creates a copy of the underlying array (attention: it's a copy of the array - not the objects in the array!).

The latter creates an ArrayList that points to the same array as the original object.

Example:

    String[][] s1 = {{new String("a"), new String("b")}};
    String[][] s2 = s1.clone();
    System.out.println(Arrays.toString(s1)); // prints [[Ljava.lang.String;@7440e464]
    System.out.println(Arrays.toString(s2)); // prints [[Ljava.lang.String;@7440e464]
    System.out.println(s1[0] == s2[0]); // prints true
    System.out.println(s1 == s2); // prints false - because s2 != s1

Since the array of arrays is an object, and the item in the first place (s[0]) is an array itself (which is an object as well) - you can see that clone simply copied the reference to the objects.

Sign up to request clarification or add additional context in comments.

4 Comments

Okay, but with second code. When I add something to ArrayList that was cloned, the clone does not get affected.
@TobiasJohansson clone "copies" only primitive types, as for object members it only copies the reference. What you describe is weird and I need to see the full implementation in order to figure out why that's the behavior your'e seeing.
But when I modify existing data the methods gives different results! Now I see. This with pointers and references is so much easier in C.
@TobiasJohansson I'll add some code to the answer to demonstrate the issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.