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) ..
Asked
Active
Viewed 275 times
0
-
Linked List of Linked Lists? It's possible, yes ... – AntonH Nov 14 '17 at 20:30
-
Java's missing Tuple... – michid Nov 14 '17 at 20:31
-
1You need to define a class to contain the x and y values of each dimension, let's call it `Dimension`, and then declare a `List
`. – Jim Garrison Nov 14 '17 at 20:32 -
You probably don't want to be storing anything in an actual LinkedList. – pvg Nov 14 '17 at 20:35
-
Possible duplicate of [*Does Java SE 8 have Pairs or Tuples?*](https://stackoverflow.com/q/24328679/230513) – trashgod Nov 14 '17 at 21:43
2 Answers
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
-
-
1FYI: `Dimension` is typically used to represent size - especially considering there's already a `Dimension` class in the API – MadProgrammer Nov 14 '17 at 21:16
-
-