这可能是最直接的方法:
public void mousePressed() {
int lastProcessedI = -1;//Or some reasonable default value
for (int i = 0; i < 6; i++) {
System.out.println("On iteration " + i + " having completed " + lastProcessedI);
if (mouseX > boxes[i].x) {
boxes.[i].openIt();
}
lastProcessedI = i;//mark i as having been processed.
}
}
请注意,lastProcessedI
应该在您的 for 循环之前声明,否则循环迭代时它将超出范围。通过在 for 循环之前声明它,它会停留在mousePressed
方法的包含范围内。
如果您需要维护更多历史记录,这也很容易实现:
public void mousePressed() {
List<Integer> processHistory = new ArrayList<Integer>();
for (int i = 0; i < 6; i++) {
System.out.println("On iteration " + i + " having completed " + processHistory.get(processHistory.size() - 1));
if (mouseX > boxes[i].x) {
boxes.[i].openIt();
}
processHistory.add(i);//store i as having been processed.
}
//here you can get any previously processed i
processHistory.get(processHistory.size() - 2);//I selected 2 arbitrarily, but it will provide the second-to-last i processed.
}