我正在Java上執行一個庫系統。我有一個課叫做東西有兩個孩子:打印和數字。 打印然後分爲書和期刊。即使使用ListIterator,ConcurrentModificationException仍然存在
以下是我寫給打印項目的代碼片段。完整的代碼有導入。
public class Collection
{
private ArrayList <Things> theCollection;
private ListIterator <Things> listItr;
public Collection()
{
theCollection = new ArrayList <>();
listItr = theCollection.listIterator();
}
public void get()
{
Things sThing;
Book sBook;
Periodical sPeriodical;
Digital sDigital;
sThing = listItr.next();
if (sThing instanceof Book)
{
sBook = (Book) sThing;
sBook.bookInfo();
}
else if (sThing instanceof Periodical)
{
sPeriodical = (Periodical) sThing;
sPeriodical.periodicalInfo();
}
else if (sThing instanceof Digital)
{
sDigital = (Digital) sThing;
sDigital.digitalInfo();
}
}
public void printAll()
{
while (listItr.hasNext())
get();
}
}
我對下面的語句有問題的get()方法:
sThing = listItr.next();
當我運行的代碼,它給了我一個ConcurrentModificationException的。我試圖在網上搜索答案,但他們都建議使用迭代器,我也有。我在哪裏做錯了?
感謝您的幫助。
如何(以及何時)將項目添加到列表中? – Mureinik
你能分享bookInfo()方法嗎? – dogankadriye