I'm doing a library system on Java. I have a class called Things with two children: Print and Digital. Print is then divided into Book and Periodical.
The following is a snippet of the code I wrote to print items. The full code has imports.
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 ();
}
}
I have a problem on the following statement in the get() method:
sThing = listItr.next ();
When I ran the code, it gave me a ConcurrentModificationException. I tried to search for answers online, but all of them suggested to use an iterator, and I have that. Where did I do wrong?
Thanks for your help.