Greetings,
I am trying to find all pairs of X,Y axis Points in Java from each point to every other point as seen below. I am using Eclipse on Windows. Much appreciate for the help on this problem.
Three point e开发者_开发百科xample: (1.0, 2.0) (2.0, 2.0) (3.0, 4.0)
All pairs from each to every other point:
Output:
(1.0 ,2.0) (2.0, 2.0)
(1.0, 2.0) (3.0, 4.0)(2.0, 2.0) (1.0, 2.0)
(2.0, 2.0) (3.0, 4.0)(3.0, 4.0) (1.0, 2.0)
(3.0, 4.0) (2.0, 2.0)Thanks,
PaulJust iterate over the list two times and exclude the ones that are same:
List<Point> points = new ArrayList<Point>();
points.add(new Point(1, 2));
points.add(new Point(2, 2));
points.add(new Point(3, 4));
printCombinations(points);
public static void printCombinations(List<Point> points) {
for (int i = 0; i < points.size(); i++) {
for (int j = 0; j < points.size(); j++) {
if (i != j)
System.out.println(points.get(i) + ":" + points.get(j));
}
}
}
Short hint: go through all points, iterate through all following points and add the pair somewhere.
精彩评论