Results 1 to 1 of 1
- 01-29-2012, 08:37 PM #1
Member
- Join Date
- Jan 2012
- Posts
- 2
- Rep Power
- 0
objects as parameters change in method
Hi, can anyone help me with this: I wrote the follow code, and expected the output to be cor=1,ver=1. however, the result is cor=0,ver=0.
I found that if I change the method findPositoin and make i and j declared and initialized in for, the result will be correct, can anyone tell me why?
class Int{
int value;
public String toString(){
return ""+value;
}
}
class JigsawPuzzle{
int[][] status = {{1,2,3},{8,0,4},{7,6,5}};
static void findPosition(Int cor,Int ver,JigsawPuzzle p){ //find the position of blank part
int i=0,j=0;
for( ;i<=2;i++)
for( ;j<=2;j++)
if(p.status[i][j]==0){
cor.value=i;ver.value=j;
return;
}
}
}
public class Step{
public static void main(String[] args){
Int cor=new Int(),ver = new Int();
JigsawPuzzle p = new JigsawPuzzle();
p.findPosition(cor,ver,p);
System.out.println("cor="+cor+",ver="+ver);
}
}
Similar Threads
-
Problem passing objects as parameters to remote methods via RMI
By nicoeschpiko in forum New To JavaReplies: 1Last Post: 02-03-2011, 05:43 PM -
how to pass parameters from a method to another which accepts to parameters?possible?
By amrmb09 in forum Advanced JavaReplies: 5Last Post: 11-21-2010, 02:08 PM -
how to use objects and its values to use in other function without using parameters
By sfaiz in forum New To JavaReplies: 3Last Post: 09-29-2010, 02:38 PM -
Need to dynamicaly get all parameters in a method call.
By DavidJohns in forum Advanced JavaReplies: 2Last Post: 05-21-2008, 07:15 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks