开发者

All pairs of X,Y coordinates from Each Point to Every other Point with Java [closed]

开发者 https://www.devze.com 2023-03-01 17:06 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

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,

Paul


Just 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消