Major array problem

Y

Yomi Smito

Why does this method alter the array "double[][] A", if the only one
whose elements are being set is "double[][] C" ? Help would be very
appreciated

static double[][] prueba (double[][] A,double[][] B){
double[][] C=new double[A.length][];
for (int i = 0; i < C.length; i++) {
C=A;
}
for (int i = 0; i < C.length; i++) {
for (int j = 0; j < C.length; j++) {
C[j]=0;
}
}
return C;
}
 
L

Lasse Reichstein Nielsen

Yomi Smito said:
Why does this method alter the array "double[][] A", if the only one
whose elements are being set is "double[][] C" ? ....

....
C[j]=0;


Arrays are objects, so when storing them, you store a reference. You
don't have two-dimensional arrays, only arrays of arrays, so when you
do the first assignment above, you make C reference the same
integer array as A. You then change the value of the j'th entry of
that array.

/L
 
Y

Yomi Smito

Lasse Reichstein Nielsen a écrit :
Yomi Smito said:
Why does this method alter the array "double[][] A", if the only one
whose elements are being set is "double[][] C" ? ...

...
C[j]=0;


Arrays are objects, so when storing them, you store a reference. You
don't have two-dimensional arrays, only arrays of arrays, so when you
do the first assignment above, you make C reference the same
integer array as A. You then change the value of the j'th entry of
that array.

/L


Ok thanks a lot. Clearly I'm new to OOP.
 
Y

Yomi Smito

I'm fixing it by using a more generic "C=new double[A.length];"
instead. Thanks.
 
D

Dusan Juretic

Yomi Smito said:
Why does this method alter the array "double[][] A", if the only one
whose elements are being set is "double[][] C" ? Help would be very
appreciated

static double[][] prueba (double[][] A,double[][] B){
double[][] C=new double[A.length][];
for (int i = 0; i < C.length; i++) {
C=A;
}
for (int i = 0; i < C.length; i++) {
for (int j = 0; j < C.length; j++) {
C[j]=0;
}
}
return C;
}

Your problem is this line: C=A, now C references A (so A and
C are now dependent).
 
B

blmblm

Lasse Reichstein Nielsen a écrit :
Yomi Smito said:
Why does this method alter the array "double[][] A", if the only one
whose elements are being set is "double[][] C" ? ...

...
C[j]=0;


Arrays are objects, so when storing them, you store a reference. You
don't have two-dimensional arrays, only arrays of arrays, so when you
do the first assignment above, you make C reference the same
integer array as A. You then change the value of the j'th entry of
that array.


Ok thanks a lot. Clearly I'm new to OOP.


I don't think this is an OOP thing so much as it is a Java thing.
In Java, all you have are primitives and references, and assignment
can't be overloaded. In C++, things are different, and they might
be in other OOP languages as well. (Actually, you would have other
problems passing a multidimensional array as a parameter in C++,
but that's further off-topic.)

Just a nitpick, possibly useful if/when you later learn another
OO language.
 
D

Dimitri Maziuk

Yomi Smito sez:
Lasse Reichstein Nielsen a écrit :
Yomi Smito said:
Why does this method alter the array "double[][] A", if the only one
whose elements are being set is "double[][] C" ?
....
Arrays are objects, so when storing them, you store a reference.
....
Ok thanks a lot. Clearly I'm new to OOP.

This has nothing to do with OOP and everything with Java's type
system and terminology. All objects are pointers, and that's what
"reference" means. When it comes to objects, the promise to pass
everything "by value" means "pass the value of the pointer".
So the assignment operator doesn't do what you expect.

Dima
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top