0

So I'm writing a program which keeps a track of various documents like e-mails, memos, and reports. The documents are stored in an ArrayList called "active" by default, but the user has the option of transferring them to another ArrayList called "archive", using an identification code("docId").

I thought that this would be pretty straightforward but I'm running into this error and would appreciate your help in resolving it. Here's my code:

private static ArrayList active = new ArrayList();
private static ArrayList archive = new ArrayList(); 

public static void archiveDocument(double docId)
{       
    if(active.isEmpty() == true)
    {
        System.out.println(Messages.emptyList());
    }
    else
    {
        for(Object a : active)
        {
            Document doc = (Document) a;

            if(doc.getIdNum() == docId)
            {
                archive.add(a);
                active.remove(a);

                System.out.printf(Messages.enteredIntoArchive(), doc.getIdNum());
            }
            else System.out.println(Messages.notFound());
        }
    }
}

3 Answers 3

3

If you want to remove during iteration, use an explicit iterator:

Iterator i = active.iterator();
while (i.hasNext()) {
  Document doc = (Document) i.next();
  if (doc.getIdNum() == docId) {
    archive.add(doc);
    i.remove();
    System.out.printf(Messages.enteredIntoArchive(), doc.getIdNum());
  }
  else
    System.out.println(Messages.notFound());
}
Sign up to request clarification or add additional context in comments.

Comments

3

You are trying to change the list while iterating over its enumerator.

for(Object a : active)

This starts an enumeration

active.remove(a);

You modify it here.

One easy fix is to copy the list before enumerating over it and then enumerate over the copy.

ArrayList activeCopy = new ArrayList(active);
for(Object a : activeCopy)
{
...
}

Comments

2

You can't modify the enumeration while reading at same time. You need to make a copy of the ArrayList. Sometimes I shortcut by converting the ArrayList into an array[].

public void archiveDocument(double docId) {
        if (active.isEmpty() == true) {
            System.out.println(Messages.emptyList());
        } else {
            for (Object a : active.toArray(new Object[0])) {
                Document doc = (Document) a;

                if (doc.getIdNum() == docId) {
                    archive.add(a);
                    active.remove(a);

                    System.out.printf(Messages.enteredIntoArchive(), doc
                            .getIdNum());
                } else
                    System.out.println(Messages.notFound());
            }
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.