0

thats part of my code:

public List<Integer> _list = new ArrayList<>();

public void removeInteger(Integer i)
{
    _list.remove(i);
}

public class CheckThread implements Runnable
{
    @Override
    public void run()
    {
        synchronized(_list) 
        {
            Iterator<Integer> it=_list.iterator(); 
            while(it.hasNext()) 
            {
                Integer i = it.next();
            }
        }
    }
}

Thread is running all the time(didnt write that part) and when i remove from list using removeInteger method, i have got ConcurrentModificationException. Any idea how to solve that problem?

  • Where are you invoking that `removeInteger()` from? And also, if you have the `iterator`, then you can remove using iterator. Using [`Iterator#remove`](http://docs.oracle.com/javase/7/docs/api/java/util/Iterator.html#remove()) – Rohit Jain Oct 23 '12 at 12:20
  • this thread is running every x seconds and iterating over the list. Thread is being run while program starts and removeInteger method is run right behind that. I didnt want to write whole program because its too big – user1656546 Oct 23 '12 at 12:22
  • @user1656546. If you are trying to remove elements while the Thread is already iterating your List, then you can't do that. You cannot modify a list that you are iterating upon. The only way would have been to remove it from the `iterator` while loop itself through iterator reference. – Rohit Jain Oct 23 '12 at 12:24
  • yeah thats what i am trying to do, remove from the list while thread is doing something with it. Can u write how it would need to look like? – user1656546 Oct 23 '12 at 12:28

4 Answers4

2

You should use it.remove() when you need to remove an element.

Uriel Frankel
  • 14,304
  • 8
  • 47
  • 69
0

- You must synchronize remove() method, else any otherwise any other thread can access the list and try to remove element from it while other is putting element in it..

 public  synchronized void removeInteger(Integer i)
{
    _list.remove(i);
}
Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
  • tried that and also tried public void removeInteger(Integer i) { synchronize(_list) { _list.remove(i); }}. Same error – user1656546 Oct 23 '12 at 12:24
0
  • If you want to remove while iterating based on a condition write your condition inside the while loop and you can remove the objects using iterator's it.remove() like below

            Iterator<Integer> it=_list.iterator(); 
            while(it.hasNext()) 
            {
                Integer i = it.next();
                if(i<10){
                  it.remove();
                }
            }
    
Vega
  • 474
  • 2
  • 5
  • 16
0

You may try some thing like below ( you condition can be made better according to your requirements)

public void removeInteger(Integer i)
{
    Iterator iter = _list.iterator();
    int count=0;
    while(iter.hasNext()){
         if(count==i){
             _list.remove(i);
             break;
         }
    }
}
Satheesh Cheveri
  • 3,621
  • 3
  • 27
  • 46