Julia Nething
Julia Nething

Reputation: 232

Coordinate (ArrayList)

I'm having trouble formatting when using coordinates.

public class Coordinate {
  public int x;
  public int y;

  public Coordinate( int x, int y) {
    this.x = x;
    this.y = y;
  }
}

So, later, when I'm trying to find the location of my rabbit, I use:

    Coordinate (x, y) = rabbit.get(i);

And that doesn't work, but this does:

    Coordinate z = rabbit.get(i);

I want to find the x and y values so I'm confused as to how to do that and why the Coordinate (x, y) doesn't work. Thanks for your help!

Upvotes: 0

Views: 5251

Answers (1)

Simulant
Simulant

Reputation: 20102

As your attributes x,y of Coordinate are public:

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

Normaly these attriubtes are private and you access them with a getter/setter-Method:

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

I assume you use Java, so I added the Tag, this enables highlighting. This works with other languages in the same way.

Upvotes: 2

Related Questions