import java.util.*; import java.awt.*; import javax.swing.*; class Foam extends ArrayList { JLabel statusBar; Foam(JLabel statusBar) { this.statusBar = statusBar; } void draw(Graphics g) { for (Circle circle : this) { circle.draw(g); } } int points = 0; void clean() { for (int i = 0; i < this.size(); i++) { // the enhanced for is not recommended here Circle c = this.get(i); if (c.neighbors.size() >= 3) { for (int j = 0; j < c.neighbors.size(); j++) { Circle n = c.neighbors.get(j); n.neighbors.remove(c); } this.points += 1; this.statusBar.setText("Total: " + this.points); this.remove(c); } } } boolean rearranges() { int count = 0; for (Circle c : this) { if (c.fall()) { count += 1; } } this.clean(); return count > 0; } }