0

Are there any drawbacks or advantages of using

public Loteria( int[] liczby) {
   this.liczby = liczby.clone();
}

instead of:

public Loteria( int[] liczby) {
   this.liczby = new int[liczby.length];
   for(int i = 0....)
       this.liczby[i] = liczby[i];
}
jb.
  • 23,300
  • 18
  • 98
  • 136
Yoda
  • 17,363
  • 67
  • 204
  • 344
  • 1
    Note that you can also use [`Arrays.copyOf`](http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#copyOf\(int[],%20int\)) (which wraps a call to [`System.arraycopy`](http://docs.oracle.com/javase/7/docs/api/java/lang/System.html#arraycopy\(java.lang.Object,%20int,%20java.lang.Object,%20int,%20int\)) with type safety). – Paul Bellora May 19 '13 at 18:28
  • related: [Is there any reason to prefer System.arraycopy() over clone()?](http://stackoverflow.com/questions/7179251/is-there-any-reason-to-prefer-system-arraycopy-over-clone) – Paul Bellora May 19 '13 at 18:38
  • It's one line of code instead of three. – user207421 May 19 '13 at 23:49

3 Answers3

2

Both approaches create shallow copies of the array elements, which means the elements inside your arrays will still reference each other. If you want shallow copies, stick to array.clone().

  • More readable
  • Less code, uses standard Java API.

Deep copying arrays

Java 6+

this.liczby = Arrays.copyOf(liczby, liczby.length);

Older versions

System.arraycopy(liczby, 0, this.liczby, 0, liczby.length);

Test

Object[] original = { new Object(), null };
Object[] copy = new Object[2];
System.arraycopy(original, 0, copy, 0, original.length);
Object[] copy2 = Arrays.copyOf(original, original.length + 1);
copy2[1] = 2;
System.out.println(original[1]); // null
System.out.println(copy2[1]); // 2
flavian
  • 28,161
  • 11
  • 65
  • 105
  • It's an array of primatives. – Antimony May 19 '13 at 18:25
  • @alex23 When dealing with primitives, you are passing around their real values, and not the values of references, so creating a new instance of an array and assigning directly the values suffices for a deep clone. An array of objects would require cloning of the objects also. – afsantos May 19 '13 at 18:32
  • -1 `Arrays.copyOf` does not do a deep copy. Anyway, we're talking about an `int[]` here so it's besides the point. – Paul Bellora May 19 '13 at 18:36
  • So `Arrays.copyOf` makes a deep copy? If not why or if it is better than clone? – Yoda May 19 '13 at 18:42
  • What is the deep copy I remember sth from c++ but not really? And why you pass 0 not array.length? – Yoda May 19 '13 at 18:43
  • Ok but if the clone() makes shallow copy any change in orignal array will change the copy. – Yoda May 19 '13 at 18:49
  • 1
    Of course `System.arraycopy` yields the same result - `Arrays.copyOf` is making a call to it under the hood (as I already said in my comment on the question). You can see the source [here](http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/7-b147/java/util/Arrays.java#Arrays.copyOf%28java.lang.Object%5B%5D%2Cint%2Cjava.lang.Class%29). At this point it seems like you're just confused about what "deep copy" means. Basically, a deep copy would also copy the underlying object, hence comparing the elements with reference equality (`==`) would be false. – Paul Bellora May 19 '13 at 19:05
  • 1
    http://stackoverflow.com/questions/64036/how-do-you-make-a-deep-copy-of-an-object-in-java, http://stackoverflow.com/questions/3291830/deep-copy-and-arraylist-java/3291857 – fgb May 19 '13 at 19:38
  • 1
    That's the point, it's not a deep clone. No one said that the arrays are the same reference. How about explaining why replacing your example with `copy = (Object[])original.clone();` produces the same result. – fgb May 19 '13 at 19:49
  • 1
    1) Your test only shows a shallow copy. "both outputs would be 2" would only happen if even the array itself wasn't copied. The array is copied, but the element objects are not. The first elements of `original`, `copy`, and `copy2` are all references to the same `Object`. 2) The article you linked is irrelevant because my example only uses `==` to compare `Object`s, not boxed primitives (like you decided to for some reason). 3) The only way to deep-clone an array without a loop is to use serialization (this is discussed in one of the posts linked by fgb). – Paul Bellora May 19 '13 at 21:18
1

First off, the first version is much shorter and more readable. This is always a big advantage when programming.

As for performance, you should always profile first and only worry if you see a bottleneck. But on a modern VM, they'll probably end up being equally fast (though again, you need to profile if it's important). Array cloning is usually implemented via an intrinsic, but the VM can probably recognize that the second version is equivalent.

Antimony
  • 37,781
  • 10
  • 100
  • 107
  • @alex23 The second version the OP provides is a deep copy, so my guess would be that he is looking for a deep copy. – afsantos May 19 '13 at 18:29
0

With respect to your hand-written version,

PROS: Gives you more control about your code

CONS: Is harder to read and maintain. Has no performance advantage over generic clone().

I would go with clone() as long as you can.

likeitlikeit
  • 5,563
  • 5
  • 42
  • 56