【发布时间】:2015-08-23 19:29:55
【问题描述】:
我正在做一个 Java 库系统。我有一个名为 Things 的班级,有两个孩子:Print 和 Digital。 Print又分为Book和Periodical。
以下是我为打印项目而编写的代码的 sn-p。完整的代码有导入。
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。我试图在网上搜索答案,但他们都建议使用迭代器,我有。我哪里做错了?
感谢您的帮助。
【问题讨论】:
-
如何(以及何时)将项目添加到列表中?
-
能分享一下 bookInfo() 方法吗?
标签: java