IndexOutOfBoundsException when iterating through two ArrayLists
in
Programming Questions
•
3 years ago
Hello,
I am working on a sketch which involves creating and moving two different types of geometric shapes about the screen. Currently displayed shapes are being held in two ArrayLists, forms1 and forms2. When two different types overlap, I want them to disappear and I have written the following function to do so:
- void checkIntersect() {
- for (int i = forms1.size() - 1; i >= 0; i--) {
- for (int j = forms2.size() - 1; j >= 0; j--) {
- Circle form1 = (Circle) forms1.get(i);
- Line form2 = (Line) forms2.get(j);
- float formActualDist = dist(form1.xpos, form1.ypos, form2.originX, form2.originY);
- float formMinDist = (form1.diameter/2) + (form2.diameter/2);
- if (formActualDist <= formMinDist) {
- forms1.remove(i);
- forms2.remove(j);
- }
- }
- }
- }
1