Reputation: 510
How do I access an array of objects inside of an array of objects?
my code:
private boolean intersect(Polygon[] polygons, Line[] the_path, int i, int j)
{
int k = 0;
boolean intersect;
if(intersect == true)
{
for(i = 0; i < polygons.length; i++)
for(j = 0; j < polygons._lines.length; j++)
for(k = 0; k < the_path.length; k++)
intersect = polygons._lines[j].intersect(the_path[k]);
}
return intersect;
}
The intersect method in the array of lines returns a boolean, but there is a separate array of line objects in each of the polygons....how do I go about accessing that method? (note..I don't know if this exact code will do what I want yet, but either way I need to be able to access that method)
Upvotes: 0
Views: 80
Reputation: 4748
I think you accidentally left out the index into polygons (e.g. polygons[i]
). Also, currently you have intersect
being assigned the value of intersect()
which means it is overwriting any other values given to the boolean intersect
in previous loop iterations. I have added an if
statement that will break out of the function immediately if that case if found instead. However, you could instead do something like intersect = intersect || ... .intersect()
if you want to keep that variable.
Try this:
private boolean intersect(Polygon[] polygons, Line[] the_path, int i, int j) {
int k = 0;
for (i = 0; i < polygons.length; i++) {
for (j = 0; j < polygons[i]._lines.length; j++) {
for (k = 0; k < the_path.length; k++) {
if (polygons[i]._lines[j].intersect(the_path[k])) {
return true;
}
}
}
}
return false;
}
Upvotes: 1