我有这个代码:
for(int i = 0; i < oldCars.size(); ++i)
{
if(oldCars.get(i).getVelocity().length() > 0.0f)
{
oldCars.get(i).update(timeStep);
}
else
{
oldCars.remove(i);
}
}
它似乎工作正常,但它会在所有条件下工作吗?
谢谢
最佳答案
迭代时如果要移除,从末尾开始:
for(int i = oldCars.size() - 1; i >= 0; i--)
{
if(oldCars.get(i).getVelocity().length() > 0.0f)
{
oldCars.get(i).update(timeStep);
}
else
{
oldCars.remove(i);
}
}
关于java - 我可以在迭代时从列表中删除吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13546647/