Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How can i add Object into ArrayList after assigning Iterator

807600Sep 5 2007 — edited Sep 6 2007
Hi there,

Please refer following code snippet.

1 List arrayList = new ArrayList();
2 arrayList.add(new Integer(24));
3 arrayList.add(new Double(2));
4 arrayList.add(new Employee( Employee.getNewEmployeeId(), "Manager");
5 ListIterator iterator = arrayList.listIterator();
6 arrayList.add(new Employee(Employee.getNewEmployeeId(), "Engineer");


This code throws java.util.ConcurrentModificationException as i am trying to add Object after assigning iterator to the List. I found that we have to use add method of iterator, so if i change the Line 6 with........

6 iterator.add(new Employee(Employee.getNewEmployeeId(), "Engineer");

...............the code complies and runs fine, but (when i itrate through the arrayList ) it does not show the Object added at Line 6 above.

Can somebody tell me whats going wrong as i am new to JAVA? Thanks in advance.

( PM: toString() method is overrided in Employee to print every field,
Constructor for Employee is defined to take two arguments (int, String)
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 4 2007
Added on Sep 5 2007
8 comments
237 views