• Post Reply Bookmark Topic Watch Topic
  • New Topic

Int[][] magically changes  RSS feed

 
Atrus Greyor
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A Puzzle is just the old 8 puzzle where there is one empty space and you have to get them in order. So, the Puzzle class just has a 2d int array. What I'm wondering is why they are being difficult.
With this code:



Whenever I run it between the puzzle p, changes. The first printout of p call upon with "System.out.println("First P \n" + p);" differs from the second printout of p, called upon with "System.out.println("P \n" + p);". I have no idea as to why it changes, as I am doing NO operations upon P.

Thanks for the help in advance.
 
Joe Ess
Bartender
Posts: 9439
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Atrus Greyor:
Whenever I run it between the puzzle p, changes. The first printout of p call upon with "System.out.println("First P \n" + p);" differs from the second printout of p, called upon with "System.out.println("P \n" + p);".


This code will not compile. The variable puz on line 47 is undeclared. Please Post Real Code.
Since p in both println calls is passed in from another class, we really can't evaluate your claim that it is not modified between the call to the constructor and the call to moveDown(). You need to Isolate the problem and Tell the Details.
 
Atrus Greyor
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is real code, I was only attempting to reduce the number of relevant lines as stated in Tell the Details. Puz was only an attempt to get it to stay the same. It has been removed in the real code.

The changed code (with p = puz removed is below.)



As well here is the main driver class.



And all that does is create new random puzzle..
 
Joe Ess
Bartender
Posts: 9439
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And the Puzzle class looks like. . .
 
Tim LeMaster
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There may be other things wrong here - without seeing more however



does a shallow copy of an int[][]. Such that each element of temp points to the same int[] as p. That is



is true. Then later you do



Which modifies the int[] elements which is pointed to by both temp and p.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!