以下は、私が持っていた宿題の解決策の一部であり、なぜ彼が 2 つの for ループを持っているのかわかりません。それが私だったら、if (carsParked[i] == c) を設定してから、carsParked[i]=null を設定します。なぜ 2 番目の for ループ ステートメントを誰か説明できるのかわかりません。
ちなみに、CarsParked 配列は、駐車中の車のオブジェクトを格納する Car クラスの一種です。
public void driveOut(Car c)
{
for (int i=0; i<carsParked.length; i++) // Loop through the carParked array
{
if(carsParked[i] == c) // Find Car c at index i
{
//carsParked, remove(c);
for (int j=i; j<carsIn-1; j++)
{
carsParked[j] = carsParked[j+1];
}
carsParked[carsIn-1] = null;
carsIn = carsIn - 1;
}
}
}
こんな感じで車が停められます
public void driveIn(Car c)
{
if(carsIn < carsParked.length)
{
carsParked[carsIn] = c;
carsIn = carsIn + 1;
}
else // error message
{
System.out.println("Park " + location + " is full, for " + c);
}
}