How can i add Object into ArrayList after assigning Iterator
807600Sep 5 2007 — edited Sep 6 2007Hi 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)