2

After some search for copying of multidimensional arrays manipulation in java I found this question: copy a 2d array in java and a downvoted answer suggests using the method in the title. So my questions are:

1) why is it bad? (becuase it looks quick and obvious)

2) is the best way still System.arraycopy each line?

Community
  • 1
  • 1
Alex
  • 944
  • 4
  • 15
  • 28
  • 2
    because it is **shallow copy** as far as I know – Kick Buttowski Oct 19 '14 at 17:09
  • If you need speed and this really is your main bottleneck, try to avoid multidimensional arrays altogether. – biziclop Oct 19 '14 at 17:12
  • @biziclop: so if I have, say, m parameters for n individuals instead of an array with N rows and M columns just have a NxM string? How will it help? – Alex Oct 19 '14 at 17:13
  • @KickButtowski: could you explain what's wrong with a shallow copy? – Alex Oct 19 '14 at 17:16
  • 1. http://stackoverflow.com/questions/5563157/how-to-deep-copy-2-dimensional-array-different-row-sizes 2. https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=shallow+copy+vs+deep+copy+java – Kick Buttowski Oct 19 '14 at 17:17
  • @Alex cheack my links – Kick Buttowski Oct 19 '14 at 17:18
  • So the only way to create a deep copy is to loop over every element? – Alex Oct 19 '14 at 17:20
  • I still don't see why System.Arraycop() or copyOf() is better than clone() for mn arrays – Alex Oct 19 '14 at 17:21
  • @Alex No, don't do that under any circumstances. What I tried to say was that if this **really** is what slows your application down (and 99% of the time it's something else), you might consider using a one-dimensional array with NxM elements. – biziclop Oct 19 '14 at 17:25
  • @biziclop: no, honestly, my application runs quite fast, but I use array.clone() which seems like an easy way to get a copy of an array. Then I suddenly read (in the question I referred to) that it's a bad idea. So I'm confused now. – Alex Oct 19 '14 at 22:02
  • @biziclop: I mean the last downvoted answer – Alex Oct 19 '14 at 22:03

1 Answers1

1

Fundamentally, an array is an object. If you have a multidimensional array and you clone it, you won't get copies of the internal arrays (because they are also array Object(s)). You could use Arrays.copyOf(T[],int). From the linked Javadoc,

Copies the specified array, truncating or padding with nulls (if necessary) so the copy has the specified length. For all indices that are valid in both the original array and the copy, the two arrays will contain identical values. For any indices that are valid in the copy but not the original, the copy will contain null. Such indices will exist if and only if the specified length is greater than that of the original array. The resulting array is of exactly the same class as the original array.

Edit

The above is analogous to System.arraycopy() but also creates shallow copies. If you need to create deep copies of the multidimensional array you would have to iterate over the array clone and create a deep copy for each row.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • 4
    You should clarify that copyOf is also a shallow copy -- whether they use System.arraycopy or copyOf, they need to iterate over the cloned multidimensional array and copy the array in each slot. – Jeffrey Bosboom Oct 19 '14 at 17:14
  • I still don't see why copyOf() or ArrayCopy() are better than clone() for md arrays - if all of them are shallow – Alex Oct 19 '14 at 17:21
  • You asked if one was *best*. Technically they're all *best* in that they perform the same function. – Elliott Frisch Oct 19 '14 at 17:23
  • I'm lost: if there's no difference, why bother writing a whole loop over each line instead of one line using clone() if the result is exactly the same? – Alex Oct 19 '14 at 17:24
  • @Alex Consider, *what if* your `Object` instances were immutable? – Elliott Frisch Oct 19 '14 at 17:26
  • Thanks, but I'm still at loss - what's the advantage of using arraycopy() or copyOf() vs array.clone() – Alex Oct 19 '14 at 21:58