[英]Can someone explain how this method works?
該方法即將刪除人口最少的城市!
方法:
public void delCity(long population) {
if (population== 0) {
System.out.println("There is no city!");
return;
}
for (int i = 0; i < index; i++) {
if (cities[i].getPopulation() < population) {
for (int j = i; j < index - 1; j++) {
cities[j] = cities[j + 1];
}
cities[--index] = null;
i--;
}
}
}
所以我不了解的部分是第二個for循環的主體,例如, cities[j] = cities[j + 1];
工作原理, cities[--index] = null; i--;
cities[--index] = null; i--;
? 非常感謝您的回復。
cities[j] = cities[j + 1];
上面的語句將索引j
的城市設置為索引j + 1
的城市。
cities[--index] = null;
該語句僅將城市的最后一個索引設置為null
。
因此,不會刪除任何元素。 要刪除的元素只是被下一個元素覆蓋。 循環將對要刪除的元素之后的所有元素執行此操作。 最后,剩下最后一個設置為null
元素。
拋開解釋,代碼效率很低。
我評論了該代碼,因為在這種簡單的上下文中,任何更詳盡的解釋都是多余的。
//Scan all the cities, from i = 0 to i = index -1
for (int i = 0; i < index; i++)
{
//Check it cities[i] has LESS population than specified
if (cities[i].getPopulation() < population)
{
//We need to remove cities[i]
//We shift all the subsequent cities
//(cities[i+1], cities[i+2], ... cities[index-1] one position LEFT,
//thereby overwriting cities[i] and so deleting it
//Shift all the subsequent cities left
//We start this for FROM i, so the first iteration is
//cities[i] = cities[i + 1];
//The second is
//cities[i + 1] = cities[i + 2];
//and so on. We STOP AT index - 2 (note the strict less than)
//So that j + 1 is AT MOST index - 1
for (int j = i; j < index - 1; j++)
{
cities[j] = cities[j + 1];
}
//Since j was at most index - 2, the cities[index - 1] was not
//overwritten with the next one. Naturally as there is no next one
//for it.
//This last city is now duplicated (the copy is at index - 2) and
//Must be overwritten with null.
//Also index, which keep track of the number of cities must be
//decremented (here the pre-decrement -- is used)
cities[--index] = null;
//cities[i] was the deleted city. But now it contains cities[i + 1]
//Which is a totally different city!
//If we continue the for now, we will go to the NEXT city, as i
//will be incremented, to keep i the same one more iteration, we
//decrement it. This way we process (the new) cities[i] one more time.
i--;
}
}
使用舊鉛筆和橡皮,直到您完全理解算法為止,實際上,沒有更好的方法。
index
是陣列cities
的大小。 因此,當您到達cities[i].getPopulation() < population
為true
特定位置時,會將所有城市從位置i + 1
移至數組index - 1
的末尾,再移至位置i
到index - 2
,用以下方法做到這一點:
for (int j = i; j < index - 1; j++) {
cities[j] = cities[j + 1];
}
因此,位置i
上人口少的城市(現在為j
)將通過用citiescities[j + 1]
覆蓋城市而被刪除,而citiescities[j + 1]
將被citiescities[j + 2]
覆蓋, citiescities[j + 2]
,直到最后的數組。 因此,在位置上citiescities[index - 2]
將與citiescities[index - 1]
,因此我們需要將其刪除,因此您將citiescities[index - 1]
cities[--index] = null;
,其中index
將首先遞減(這就是為什么--
將從index
保留),而cities[oldIndex - 1]
將為null
。 這也將有助於for
循環不會去,直到第一index
在數組值的位置,但直到在最后一個城市array
,這是存在的。
現在,由於索引i將在下一個循環步驟中遞增,但是您將下一個城市移至位置i,因此必須遞減i
以檢查該城市,這就是為什么i
在末尾。
請注意,有了這個,您仍然會像以前一樣擁有一個完整的長數組,因此,如果某些城市的人口少於上述說明,則用一些空值填充。 也許更好的方法是將其復制到具有for
末尾的size index
新數組for
(注意:從中刪除項目時, ArrayList
執行類似的操作)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.