我有這種方法可以刪除工人,如果他的名字以從removeWorker()
方法接受的指定字母開始。有人可以解釋第二個for
循環是如何工作的嗎?刪除數組並刪除工作人員後管理空間
public void removeWorker(String s) {
if (index == 0) {
System.out.println("There is any worker in array!");
return;
}
for (int i = 0; i < index; i++) {
if (worker[i].getName().startsWith(s)) {
for (int j = i; j < index - 1; j++) {
worker[j] = worker[j + 1];
}
worker[--index] = null;
i--;
}
}
}
索引變量代表什麼,你是否初始化它? – theVoid