1 回答

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超4個(gè)贊
使用增強(qiáng)的for循環(huán),您在內(nèi)部使用List對(duì)象的迭代器。
在迭代基礎(chǔ)列表時(shí),不允許對(duì)其進(jìn)行修改。這就是ConcurrentModificationException您現(xiàn)在所經(jīng)歷的。
使用標(biāo)準(zhǔn)的for循環(huán),并確保在刪除元素以獲取所需功能時(shí)正確移動(dòng)索引,如下所示:
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
}
}
}
添加回答
舉報(bào)