double[][] orig=new double[][]{
{1.2,2.4,4.5,1.7},
{4.3,6.7,8.9,.2},
{3.5,6.5,7.8,4.4}
};
double[][] dest=new double[orig.length][orig[0].length];
System.arraycopy(orig,0, dest, 0, orig.length);
now if i modify an element of the dest array like
dest[0][0]=122.23;
then i find that orig[0][0] is also modified.Why is this?
But this does not happen when i copy the array using for loops as
below
for(int i=0;i<orig.length;i++){
for(int j=0;j<orig[0].length;j++){
dest[i][j]=orig[i][j];
}
}
Here, changing dest elements does not affect orig elements.
Can someone tell me if i can reproduce this effect using any array
copy methods in the API?
thanks
jim
> hello
> i have a double[][] which i copy to another using System.arraycopy()
>
> double[][] orig=new double[][]{
> {1.2,2.4,4.5,1.7},
> {4.3,6.7,8.9,.2},
> {3.5,6.5,7.8,4.4}
> };
>
> double[][] dest=new double[orig.length][orig[0].length];
>
> System.arraycopy(orig,0, dest, 0, orig.length);
>
> now if i modify an element of the dest array like
> dest[0][0]=122.23;
>
> then i find that orig[0][0] is also modified.Why is this?
Apparently references to the doubles are copied, not contents.
> But this does not happen when i copy the array using for loops as
> below
>
> for(int i=0;i<orig.length;i++){
> for(int j=0;j<orig[0].length;j++){
> dest[i][j]=orig[i][j];
> }
> }
> Here, changing dest elements does not affect orig elements.
> Can someone tell me if i can reproduce this effect using any array
> copy methods in the API?
> thanks
> jim
According to [1], you can use Arrays.copyOf[2] since version 6. Maybe
this helps. Another possible problem is with it being a
multi-dimensional array.
[1] <http://www.developer.com/java/data/article.php/3680241>
[2] <http://java.sun.com/javase/6/docs/api/java/util/Arrays.html>
--
Sabine Dinis Blochberger
Op3racional
www.op3racional.eu
You have deep copied the outer array,
but shallow copied the inner arrays.
BugBear
jimgardener schreef:
| hello
| i have a double[][] which i copy to another using System.arraycopy()
|
| double[][] orig=new double[][]{
| {1.2,2.4,4.5,1.7},
| {4.3,6.7,8.9,.2},
| {3.5,6.5,7.8,4.4}
| };
|
| double[][] dest=new double[orig.length][orig[0].length];
|
| System.arraycopy(orig,0, dest, 0, orig.length);
|
| now if i modify an element of the dest array like
| dest[0][0]=122.23;
Seems very unlikely. Why don’t you provide us with an SSCCE, and we’ll see.
H.
- --
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.9 (GNU/Linux)
Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org
iEYEARECAAYFAkiJtaYACgkQBGFP0CTku6PLKwCeJ8vQ7K4zsjtDjBDl22JE5Xb/
gG4AnRK9NFVU52epEDwoDhYLzrxBJwhX
=12uM
-----END PGP SIGNATURE-----
This copies an array of 'double []'.
>> now if i [sic] modify an element of the dest array like
>> dest[0][0]=122.23;
>>
>> then i [sic] find that orig[0][0] is also modified.Why is this?
Sabine Dinis Blochberger wrote:
> Apparently references to the doubles are copied, not contents.
"Apparently" not. What constitutes a "reference to a double", given that
double is a primitive and doesn't have references to it?
What's actually copied is the contents of the double [] []. Each element of
that array is a reference to a double [].
> According to [1], you can use Arrays.copyOf[2] since version 6. Maybe
This will still copy references to 'double []'.
> this helps. Another possible problem is with it being a
> multi-dimensional array.
Actually, there are no multi-dimensional arrays in Java. The 'double[][]' is
a single-dimensional array.
--
Lew
ok..here is,
public class CopyDemo{
public static void main(String[] args){
double[][] orig=new double[][]{
{1.2,2.4,4.5,1.7},
{4.3,6.7,8.9,.2},
{3.5,6.5,7.8,4.4}
};
double[][] dest=new double[orig.length][orig[0].length];
System.arraycopy(orig,0, dest, 0, orig.length);
debug("orig[0][0]="+orig[0][0]);
debug("dest[0][0]="+dest[0][0]);
debug("after modifying dest[0][0]:");
dest[0][0]=123.22;
debug("dest[0][0]="+dest[0][0]);
debug("orig[0][0]="+orig[0][0]);
}
public static void debug(String msg){
System.out.println(msg);
}
}
jim
jimgardener schreef:
| On Jul 25, 4:14 pm, Hendrik Maryns <gtw37b...@sneakemail.com> wrote:
|> Seems very unlikely. Why don’t you provide us with an SSCCE, and
we’ll see.
|
|
| ok..here is,
<snip fine SSCCE>
Thanks, but as others already pointed out, Java does not really have
two-dimensional arrays. A two-dimensional array really is an array of
arrays. System.arrayCopy only shallowly copies the outer array, so that
if you change the inner ones, the changes will indeed be visible in the
copied one. You’d have to copy the inner arrays as well.
Read up on ‘Java Arrays are Objects’.
H.
- --
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.9 (GNU/Linux)
Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org
iEYEARECAAYFAkiJ+TwACgkQBGFP0CTku6NqwQCeOcyInX6f19Ezo+/ml4Cx/mTL
TJUAoMKLNR5N1mVwBeOnMNgjAJMYhp1G
=nAE8
-----END PGP SIGNATURE-----
Lew is correct here. Java does not use true multi-dimensional arrays,
so anything of type "double[][]" is actually just an array of arrays.
By passing the array "orig" into System.arraycopy(...), you're making
a deep copy (1 level deep, of course) of "orig", meaning you're just
copying references to the individual double arrays within it.
jimgardner wrote:
> But this does not happen when i copy the array using for loops as
> below
> for(int i=0;i<orig.length;i++){
> for(int j=0;j<orig[0].length;j++){
> dest[i][j]=orig[i][j];
> }
> }
In the above case, you're performing the appropriate deep copy (2
levels deep) of your "orig" array. The Java API does not provide a
method to do this for you. If you absolutely must use a method in the
Java library similar to System.arraycopy(...), you could do something
like this to achieve the desired effect:
for (int i = 0; i < orig.length; i++)
System.arraycopy(orig[i], 0, dest[i], 0, orig[i].length);
--
matt
The problem is that arraycopy does a shallow copy. Basically, what you
have is an array of arrays.
So, you create a new double[][], array copy will copy the *reference* to
each of the "double[]" arrays.
You need to do a deep copy, which involves a loop:
double dest[][] = new double[orig.length][];
for (int i = 0; i < orig.length; ++i) {
dest[i] = new double[orig[i].length];
System.arraycopy(orig[i], 0, dest[i], orig[i].length);
}
Hope this helps,
Daniel.
--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
Well the original post was not an SSCCE, but it contained
enough code to clearly show what the problem was.
Everybody except you found the problem without an SSCCE.
So I assume you will read up on ‘Java Arrays are Objects’ ...
Arne
>
> Sabine Dinis Blochberger wrote:
> > Apparently references to the doubles are copied, not contents.
>
> "Apparently" not. What constitutes a "reference to a double", given that
> double is a primitive and doesn't have references to it?
>
> What's actually copied is the contents of the double [] []. Each element of
> that array is a reference to a double [].
>
>
> > According to [1], you can use Arrays.copyOf[2] since version 6. Maybe
>
> This will still copy references to 'double []'.
>
> > this helps. Another possible problem is with it being a
> > multi-dimensional array.
>
> Actually, there are no multi-dimensional arrays in Java. The 'double[][]' is
> a single-dimensional array.
>
>
Yep, I tripped over the "multi dimensional" property of the problem.
Thanks for correcting.