Java 坐标(ArrayList)
我在使用坐标时无法格式化Java 坐标(ArrayList),java,arraylist,coordinates,Java,Arraylist,Coordinates,我在使用坐标时无法格式化 public class Coordinate { public int x; public int y; public Coordinate( int x, int y) { this.x = x; this.y = y; } } 因此,稍后,当我试图找到我的兔子的位置时,我使用: Coordinate (x, y) = rabbit.get(i); 这不起作用,但这确实起作用: Coordinate z = ra
public class Coordinate {
public int x;
public int y;
public Coordinate( int x, int y) {
this.x = x;
this.y = y;
}
}
因此,稍后,当我试图找到我的兔子的位置时,我使用:
Coordinate (x, y) = rabbit.get(i);
这不起作用,但这确实起作用:
Coordinate z = rabbit.get(i);
我想找到x和y的值,所以我不知道怎么做,为什么坐标(x,y)不起作用。谢谢你的帮助 由于
坐标的属性x、y是公共的
:
Coordinate z = rabbit.get(i);
int xCor = z.x; //this is your x coordinate
int yCor = z.y; //this is your y coordinate
通常,这些属性是私有的,您可以使用getter/setter方法访问它们:
public class Coordinate {
private int x;
private int y;
public Coordinate( int x, int y) {
this.x = x;
this.y = y;
}
public int getX(){
return this.x;
}
public void setX(int newX){
this.x = newX;
}
//same for Y
}
//in the main program.
Coordinate z = rabbit.get(i);
int yourX = z.getX() //this is your x coordinate
int yourY = z.getY() //this is your y coordinate
我假设您使用Java,所以我添加了标记
,这将启用高亮显示。这同样适用于其他语言。您没有告诉我们您使用的语言,也没有发布get函数。