SouvikMaji
SouvikMaji

Reputation: 1145

concurrentModificationException in ArrayList

Problem statement: Design a system for the following scenario:

1.An item list contains item code, name, rate, and quantity for several items.

  1. Whenever a new item is added in the list uniqueness of item code is to be checked. Register a new product with its price.

  2. Time to time rate of the items may change.

  3. Whenever an item is issued or received existence of the item is checked and quantity is updated.

  4. In case of issue, availability of quantity is also to be checked.

  5. User may also like to know price/quantity available for an item.

  6. Find how many items cost more than a given amount. The amount will be a parameter.

  7. Remember that the methods have to return an error code if for example an invalid item code is given

For the above problem i have created a Item class with following members:

private String name;
private double rate;
private long code;
private int quantity;

public Item()
public Item(String name, double rate, long code, int quantity)
public Item(Item item) 
public String toString()
public String getName() 
public void setName(String name) 
public double getRate()     
public void setRate(double rate) 
public long getCode()
public void setCode(long code) 
public int getQuantity()
public void setQuantity(int quantity)

Now I have created a Shop class to access the Item class, to do all operations on a Item... Here is a part of the shop class.

    private ArrayList<Item> ItemList;
    private Iterator<Item> itr;

    public Shop() {
        System.out.println("New Shop for Items created.");
        ItemList = new ArrayList<Item>();
        itr= ItemList.iterator();
    }

    public Item search(long code) {
        Item item;
        while(itr.hasNext()) {
            item = new Item(itr.next());
            if (item.getCode() == code) {
                return item;
            }
        }
        return null;
    }

    public void addItem() throws InputMisMatchEXception{
        long aCode;
        String aName;
        double aRate;
        int aQuantity;
        Item foundItem;

            System.out.println("Enter Item code:");
            aCode = sc.nextLong();
            foundItem = search(aCode);
            if (foundItem == null) {
                System.out.println("Item name : ");
                aName = sc.next();
                System.out.println("Rate : ");
                aRate = sc.nextDouble();
                System.out.println("Quantity : ");
                aQuantity = sc.nextInt();
                Item aItem = new Item(aName, aRate, aCode, aQuantity);
                ItemList.add(aItem);
            } else if (foundItem != null) {
                System.out.println("Item exists");
            }
    }

Now when i am adding a new item, it works.. But When I add a second element it throws a

ConcurrentModificationException

Here is a sample output:
New Shop for Items created.
*

-----ITEM------
1. Add items to list
2. Update item list
3. Issue item
4. Display item details
5. Exit
Choice: 1
Enter Item code: 123
Item name : qwerty
Rate : 12345
Quantity : 100
1. Add items to list
2. Update item list
3. Issue item
4. Display item details
5. Exit
Choice: 1
Enter Item code: 1212
Exception in thread "main"
java.util.ConcurrentModificationException at
java.util.ArrayList$Itr.checkForComodification(ArrayList.java:901) at
java.util.ArrayList$Itr.next(ArrayList.java:851) at
items.Shop.search(Shop.java:28) at items.Shop.addItem(Shop.java:55)
at items.ItemDemo.main(ItemDemo.java:26)

What am i doing wrong? (Tried to give all relevant classes)

Upvotes: 7

Views: 23123

Answers (4)

Pouria Sh
Pouria Sh

Reputation: 1

you can use the iterator manually in my case using for i instead of foreach solved the problem

Upvotes: -1

Ashutosh Kumar
Ashutosh Kumar

Reputation: 21

It comes when we add the elements to the list after declaring an iterator obejct. Once try to add all the elemnts to the list and after that declare the iterator object.

Upvotes: 2

Xline
Xline

Reputation: 812

Insearch(long code) method just add

itr = ItemList.iterator();

before

 while(itr.hasNext()) 

This is because you change the list while the iterator still pointing to the old list. once you add items without updating the iterator you will get the concurrent exception.

Upvotes: 5

JB Nizet
JB Nizet

Reputation: 692281

You're ading an item to your list while iterating on it. That is not valid unless you use the iterator itself to add the new Item to the list.

Note that it's probably an error to use the same iterator everyt time the search() method is called. The iterator should probably be a local variable of the search() method rather than a field.

Upvotes: 12

Related Questions