2012-12-02 3 views
0

좌표를 사용할 때 서식을 지정하는 데 문제가 있습니다.좌표 (ArrayList)

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)가 작동하지 않는 이유에 대해 혼란 스럽습니다. 당신의 도움을 주셔서 감사합니다!

Coordinate z = rabbit.get(i); 
int xCor = z.x; //this is your x coordinate 
int yCor = z.y; //this is your y coordinate 

이 Normaly이 attriubtes이 private하고 당신이 게터/세터 메소드로 액세스 :

당신의 속성으로
+0

사용중인 언어를 알려주지 않았거나 get 기능을 게시하지 않았습니다. –

답변

2

x는, Coordinate의 y는 public 있습니다

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, Tag을 추가 했으므로 강조 표시가 가능합니다. 이 방식은 다른 언어에서도 동일하게 작동합니다.