File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes array being modified in a line of code that doesn't mention it Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "array being modified in a line of code that doesn Watch "array being modified in a line of code that doesn New topic
Author

array being modified in a line of code that doesn't mention it

Rick Gentry
Ranch Hand

Joined: Dec 14, 2002
Posts: 63


spotStatusWithCurrent and spotStatus are both int[][]. Somehow, someway, spotStatus is being modified in the line I marked. Is there some little known pointer system in java? Have I been hallucinating for days and this isn't really happening? Have demons infested my computer after playing just a bit too much doom? But seriously, someone please help, I'm going crazy trying to figure out how this is possible.
Nick George
Ranch Hand

Joined: Apr 04, 2004
Posts: 815
Arrays are objects. When you say

Object a = Object b;

they are both pointers to the same object. There is indeed a pointer system, although I'd question just how little-known it is. you have set both pointers to reference the same array. Change that array, both pointers still point to the modified object. I suggest just using a for loop to copy the array, but if you like methods with lots of parameters that you could just have easily written yourself, there's System.arraycopy. Oh, methods that don't follow conventions, tack that on too.


I've heard it takes forever to grow a woman from the ground
Warren Dew
blacksmith
Ranch Hand

Joined: Mar 04, 2004
Posts: 1332
    
    2
Arrays in Java are kind of like objects; array variables are references. When you do the line:

spotStatusWithCurrent = spotStatus

both spotStatusWithCurrent and spotStatus both refer to the same array. Modifications through either reference modify the same array, and are thus visible through the other reference as well.

Perhaps you meant to copy the array, rather than just assign a new reference to it?
Rick Gentry
Ranch Hand

Joined: Dec 14, 2002
Posts: 63
thank you thank you thank you

Why would anyone want two references to the same set of variables?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11311
    
  16

Why would anyone want two references to the same set of variables?

That's what happens when you pass an object as a parameter into a method. If i have:


while i am in my method(), i have a second reference to the same array. changes made to the array will be seen once i leave the method.

not that if i change the REFERENCE in the method:

the original array is not changed.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: array being modified in a line of code that doesn't mention it