0

I have this method that removes a specific Object P from the ArrayList

here is my code:

public void removeProduct(Product p) throws StockException{
        int flag=0;
        for(Product i:StockProducts)
            if(p.getCode()==i.getCode()){
                this.StockProducts.remove(p);
                flag=1;
            }

        if(flag==0){
                StockException e = new StockException("could not remove the Product , PRODUCT IS NOT IN THE STOCK:  ", p);
        throw e;
          }
    }

Error :

Exception in thread "main" java.util.ConcurrentModificationException
    at java.util.ArrayList$Itr.checkForComodification(Unknown Source)
    at java.util.ArrayList$Itr.next(Unknown Source)
    at Stock.removeProduct(Stock.java:29)
    at Test.main(Test.java:18)

If you need further information about my code tell me

ADDING METHOD

public void addProduct(Product p) throws StockException{
        for(Product i:StockProducts)
            if(p.getCode()==i.getCode()){
                        StockException e = new StockException("could not add the Product , CODE ALREADY EXIST IN STOCK: ", p);
                throw e;
                    }

                    this.StockProducts.add(p);  
    }
Amit G
  • 2,293
  • 3
  • 24
  • 44
Waseem Gabour
  • 39
  • 1
  • 7
  • 2
    You can't alter a List in loops or using iterator.. Instead check if your list "contains" the object , if yes, remove it and set the flag. – TheLostMind Jan 20 '14 at 11:44
  • You are getting that error because you are reading the arraylist and at the same time you are deleting the item from the list. Using iterator, You can not perform both operation simultaneously. – Rahul Jan 20 '14 at 11:44
  • I dont get it , I've done the same thing with "addProduct" method and it worked – Waseem Gabour Jan 20 '14 at 11:45
  • You CANNOT change the structure of the list while reading it using iterator or a loop. Show us your addProduct code. – TheLostMind Jan 20 '14 at 11:46
  • User an Iterator instead and move around the ArrayList using Iterator.next(). To remove an object use Iterator.remove() – kacpr Jan 20 '14 at 11:46
  • In add method you are not adding the item in list during the read time. You are adding that in the end of the list,after reading whole list. – Rahul Jan 20 '14 at 11:48
  • @WaseemGabour - You are NOT changing the structure of your list in addProduct(). structure change ~~= add/delete operations . – TheLostMind Jan 20 '14 at 11:49
  • @TheLostMind yes, I had a similar problem to which I found an answer here. It said Iterator.remove() is a safe operation . I'll try to find this edit: from Java 7 documentation: "Iterators allow the caller to remove elements from the underlying collection during the iteration with well-defined semantics." http://docs.oracle.com/javase/7/docs/api/java/util/Iterator.html – kacpr Jan 20 '14 at 11:50
  • @kacpr - Sorry.. I had misunderstood your earlier comment.. – TheLostMind Jan 20 '14 at 12:02
  • but is it logically right ? ( addProduct ) because it worked – Waseem Gabour Jan 20 '14 at 12:05

2 Answers2

5

You are deleting an object from the ArrayList while trying to iterate through it. As others have pointed out to you, this doesn't work and is giving you the ConcurrentModificationException. You want something like this:

if(StockProducts.contains(p))
   StockProducts.remove(p);

Alternatively, if you really want to iterate through the list and change it, you should be able to use a ListIterator like this:

ListIterator<Product> iter = StockProducts.listIterator();
while(iter.hasNext()){
    if(iter.next().equals(p)){
        iter.remove(p);
    }
}

or if the list can have multiple Products with the same result from getCode():

ListIterator<Product> iter = StockProducts.listIterator();
while(iter.hasNext()){
    if(iter.next().getCode() == p.getCode()){
        iter.remove(p);
    }
}
Martin Dinov
  • 8,757
  • 3
  • 29
  • 41
2

Use Iterator to remove the object from the list while iterating.

Do like this

Iterator<Product> iterator = StockProducts.iterator();
while(iterator.hasNext()) {
      Product i = iterator.next();
      if(p.getCode()==i.getCode()){
           iterator.remove();
           flag=1;
      }
}
Prabhakaran Ramaswamy
  • 25,706
  • 10
  • 57
  • 64