1 回答

TA貢獻1828條經驗 獲得超4個贊
使用增強的for循環(huán),您在內部使用List對象的迭代器。
在迭代基礎列表時,不允許對其進行修改。這就是ConcurrentModificationException您現(xiàn)在所經歷的。
使用標準的for循環(huán),并確保在刪除元素以獲取所需功能時正確移動索引,如下所示:
ArrayList<Rectangle> blocks = getBlock();
ArrayList<Rectangle> done = getDone();
outer: for(int i = 0; i < blocks.size(); ++i)
{
for(int j = 0; j < done.size(); ++j)
{
if(blocks.get(i).y == done.get(j).y + 40)
{
done.add(blocks.get(i));
blocks.remove(i);
--i; // Make sure you handle the change in index.
create();
continue outer; // Ugly solution, consider moving the logic of the inner for into an own method
}
}
}
添加回答
舉報