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 2007

Tip: Looking for answers? Try searching our database.

help with temporay variables

Thread view: 
aDeamon - 23 May 2007 15:35 GMT
When I do this
char[][] board;
// here I have some code placing data into board

// later I do this in another method that takes the former board as a
input
char[][] tmpBoard = board;
for(Point p : HashSet<Point>) {
   tmpBoard[p.x][p.y] = EMPTY;
}

For some reson both board AND tmpBoard has been changed and looks the
same. I would expect only tmpBoard to have changed - am I wrong?

/Sam
Lew - 23 May 2007 15:59 GMT
> When I do this
> char[][] board;
[quoted text clipped - 9 lines]
> For some reson both board AND tmpBoard has been changed and looks the
> same. I would expect only tmpBoard to have changed - am I wrong?

Yes.  Variables in Java (except primitives like int) are pointers to objects,
not objects themselves.  tmpBoard and board both point to the same array;
you're not changing "tmpBoard", you're changing the object to which "tmpBoard"
points, which is the same as the object to which "board" points.

board   --->{array in memory}
tmpBoard ---^

Signature

Lew

aDeamon - 23 May 2007 16:16 GMT
> > When I do this
> > char[][] board;
[quoted text clipped - 20 lines]
> --
> Lew

Tanks it help to create a new variable.
aDeamon - 23 May 2007 16:17 GMT
> > When I do this
> > char[][] board;
[quoted text clipped - 20 lines]
> --
> Lew

Tanks it help to create a new variable.
aDeamon - 23 May 2007 16:17 GMT
> > When I do this
> > char[][] board;
[quoted text clipped - 20 lines]
> --
> Lew

Thanks it help to create a new variable.
Ian Shef - 14 Jun 2007 21:21 GMT
aDeamon <stud1980@hotmail.com> wrote in news:1179930938.868379.56420
@q69g2000hsb.googlegroups.com:

> When I do this
> char[][] board;
[quoted text clipped - 11 lines]
>
> /Sam

I don't see any other replies to this one, so I will take a shot at it.
You are wrong.

The line
    tmpBoard = board
does NOT copy the contents of an array from one place to another.
'board' references (points to) an array, and now 'tmpBoard' references
(points to) the SAME array.  Changes to one are changes to the other because
there is NO other, they reference the same thing.

You need to allocate an array for 'tmpBoard' and use loops to copy from
'board' to 'tmpBoard'.  You could also use java.lang.System.arraycopy(...)
but it may not help much because your arrays have multiple dimensions.

Signature

Ian Shef     805/F6      *    These are my personal opinions    
Raytheon Company         *    and not those of my employer.
PO Box 11337             *
Tucson, AZ 85734-1337    *



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.