Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2006

Tip: Looking for answers? Try searching our database.

Major array problem

Thread view: 
Yomi Smito - 04 Jun 2006 02:02 GMT
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[i]=A[i];
       }
       for (int i = 0; i < C.length; i++) {
           for (int j = 0; j < C[i].length; j++) {
               C[i][j]=0;
           }
       }  
return C;
}
Lasse Reichstein Nielsen - 04 Jun 2006 02:18 GMT
> Why does this method alter the array "double[][] A", if the only one
> whose elements are being set is "double[][] C" ?
...
>             C[i]=A[i];
...
>                 C[i][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[i] reference the same
integer array as A[i]. You then change the value of the j'th entry of
that array.

/L
Signature

Lasse Reichstein Nielsen  -  lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
 'Faith without judgement merely degrades the spirit divine.'

Yomi Smito - 04 Jun 2006 02:53 GMT
Lasse Reichstein Nielsen a écrit :

> > Why does this method alter the array "double[][] A", if the only one
> > whose elements are being set is "double[][] C" ?
[quoted text clipped - 14 lines]
>  DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
>   'Faith without judgement merely degrades the spirit divine.'

Ok thanks a lot. Clearly I'm new to OOP.
Yomi Smito - 04 Jun 2006 02:55 GMT
I'm fixing it by using a more generic "C[i]=new double[A[i].length];"
instead. Thanks.
blmblm@myrealbox.com - 04 Jun 2006 17:21 GMT
>Lasse Reichstein Nielsen a écrit :
>
[quoted text clipped - 12 lines]
>
>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.

Signature

B. L. Massingill
ObDisclaimer:  I don't speak for my employers; they return the favor.

Dimitri Maziuk - 05 Jun 2006 17:28 GMT
Yomi Smito sez:

> Lasse Reichstein Nielsen a écrit :
>
>> > 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
Signature

Well, lusers are technically human.                            -- Red Drag Diva

Dusan Juretic - 04 Jun 2006 16:47 GMT
> 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
[quoted text clipped - 12 lines]
> return C;
> }

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

---------------
Dusan Juretic D.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.