0

I'm trying to see if I could have a java linked list of dimensions x, y ( not using hash map and not having x and y as two separate integers) I'm trying to store each square of a grid in a linked list for example (0, 1) (0, 2) ..

Ahmed
  • 1
  • 1

2 Answers2

4

Yes you can store pairs of x, y in a LinkedList<Point> as follow:

List<Point> pointList = new LinkedList<>();
pointList.add(new Point(0, 1));
pointList.add(new Point(0, 2));

Where Point is pre-defined in java.awt package. It has some useful methods such as public void move(int x, int y) or public void translate(int x, int y) which I think is useful for you.

Hope this helps.

STaefi
  • 4,297
  • 1
  • 25
  • 43
2

You have to add some class for a dimension:

public class Dimension {
    private Integer x, y;

    public Dimension(Integer x, Integer y) {
        this.x = x;
        this.y = y;
    }

    // getters/setters/equals/hashCode
}

Thus you can save dimensions into LinkedList using objects of this class:

List<Dimension> list = new LinkedList<>();
list.add(new Dimension(0, 1));
list.add(new Dimension(1, 2));
Mykola Yashchenko
  • 5,103
  • 3
  • 39
  • 48