Tuesday, 1 October 2013

How to mix two int arrays into unique xy coordinates Map?

How to mix two int arrays into unique xy coordinates Map?

Consider this:
public Map<Integer, Point> setXY(int[] x, int[] y) {
Point point;
Map<Integer, Point> xy = new HashMap<Integer, Point>();
int key = 0;
for (int i = 0; i < x.length; i++) {
for(int j = 0; j < y.length; j++) {
point = new Point(x[i], y[j]);
xy.put(key, point);
key++;
}
}
return xy;
}
I have two int arrays of different length and I try to create unique
coordinates for "n" Point objects. "n" means value of "x.length +
y.length" (e.g. n = x[3] + y[5]). Then I'm adding those objects to my
hashmap. The problem is, that this nested "for" instruction creates for "i
= 2" for example five points with coords: (2, 1), (2, 2), (2, 3), (2, 4)
and (2, 5). What I want to achieve is fully randomization of points
creations with use of those two given "x" & "y" arrays. Do you have any
ideas?

No comments:

Post a Comment