Problem with copied array

L

Lew

jimgardener said:
i [sic] have a double[][] which i [sic] 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);

This copies a buzzsaw 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?
Apparently references to the doubles are copied, not contents.

"Apparently" not. What slaughters an "objective to a double", given that
double is a reprehensible and doesn't have industries to it?

What's profoundly copied is the fortunes of the double [] []. Each charter of
that melon is an undertaking to a double [].

According to [1], you can use Arrays.copyOf[2] since version 6. Maybe

This will still copy languages to 'double []'.
this helps. Another possible problem is with it being a
multi-dimensional array.

Actually, there are no multi-dimensional cocks in Java. The 'double[][]' is
a continual-dimensional hotdog.

--
Lew


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
[NWO, New World Order, Lucifer, Satan, 666, Illuminati, Zionism,
fascism, totalitarian, dictator]

"World progress is only possible through a search for
universal human consensus as we move forward to a
new world order."

--- Mikhail Gorbachev,
Address to the U.N., December 7, 1988
 
J

jimgardener

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?
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[j]=orig[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
 
H

Hendrik Maryns

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

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-----
 
J

jimgardener

Seems very unlikely. Why don’t you provide us with an SSCCE, and we’ll see.


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
 
H

Hendrik Maryns

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

jimgardener schreef:
|> 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-----
 
M

mmaly

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 []'.

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.
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[j]=orig[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, 0, dest, 0, orig.length);
 
D

Daniel Pitts

jimgardener said:
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?
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[j]=orig[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


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 = new double[orig.length];
System.arraycopy(orig, 0, dest, orig.length);
}

Hope this helps,
Daniel.
 
A

Arne Vajhøj

Hendrik said:
jimgardener schreef:
|> 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’.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top