1

I'm trying to add new object to my ArrayList if it satisfy the condition. But it got me this ConcurrentModificationExeption when I tried to run it. Hope you could help me:

public void addTaskCollection(Task t){ 
    ListIterator<Task> iterator = this.taskCollection.listIterator();
    if(this.taskCollection.isEmpty())
        this.taskCollection.add(t);
    while (iterator.hasNext()){
        if(t.isOverlapped(iterator.next()))
            this.taskCollection.add(t);
    }    
}

And here is the exeption error

Exception in thread "main" java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:819)
at java.util.ArrayList$Itr.next(ArrayList.java:791)
at Diary.addTaskCollection(Diary.java:36)
at Test.main(Test.java:50)
Java Result: 1
2

5 Answers 5

0

Replace your code with:

ListIterator<Task> iterator = this.taskCollection.listIterator();
boolean marker = false;

if(taskCollection.isEmpty())
    this.taskCollection.add(t);
else {
   while (iterator.hasNext()) {
      if(iterator.next().isOverlapped(t) == false)
         marker = true;
   }
}

if (marker == true)
    taskCollection.add(t);

to avoid ConcurrentModificationException.

Sign up to request clarification or add additional context in comments.

6 Comments

Are you still getting ConcurrentModificationException?
Yes, and I tried to change your code a bit to get it compiled but the output is incorrect.
I finally got it right but don't have enough permission to answer my question. If possible, pls help me change update your code to mine as following: </br> ListIterator<Task> iterator = this.taskCollection.listIterator(); boolean marker = false; if(taskCollection.isEmpty()) this.taskCollection.add(t); else{ while (iterator.hasNext()){ if(iterator.next().isOverlapped(t) == false) marker = true; } if (marker == true) taskCollection.add(t); }
Because your answer is quite close with my question so I will mark you as correct answer.
Weird because if you note my code is not modifying the collection that ie being iterated. Most likely while changing you somehow got your original code back. I suggest you to post your latest code in your question again and then I can help you better.
|
0

copy the array and change the original.

3 Comments

Try to copy using Collections.copy(destiny, source).
I tried to code List<Task> tempCollection = this.taskCollection; ListIterator<Task> iterator = tempCollection.listIterator(); if(tempCollection.isEmpty()) taskCollection.add(t); while (iterator.hasNext()){ if(t.isOverlapped(iterator.next())) taskCollection.add(t); } but it doesn't work
May be you can try this: boolean modificationReqd = false; for(Task task : this.taskCollection) { if() {//check the condition here modificationReqd = true; break; } } if(modificationReqd) { this.taskCollection.add(t); }
0

It seems you encounter a race condition. Multiple threads are accessing / modifying the same collection. Use a thread-safe List implementation.

Also, you must not modifying the collection (adding / removing) while iterating on it with an Iterator.

EDIT

ConcurrentModificationExeption sounds like taskCollection is accessed and modified by multiple threads at the same time (we can not say regarding the piece of code you provide if your program is single or multi threaded). If you share taskCollection between several threads, use a thread-safe list implementation.

But, the error here is actually clearly due to the fact that you add an element to the collection between the moment you get an iterator on it and the moment you use this iterator. To fix that copy the new elements in temporary list and add them all in once at the end of the iteration.

5 Comments

So how could I modify the arraylist above, care to enlighten me?
The problem is just that you cannot modify a list whilst iterating over it with an Iterator... that's why you must copy it and modify the copy.
First, you have not enough input to say that the program is single-threaded. Anyway, we all agree that the error is due to the iteration/modification thing, and you're right to point my post was not clear. So I have just edited it. Second, here is the javadoc for ConcurrentModificationException : "... it is not generally permssible for one thread to modify a Collection while another thread is iterating over it. In general, the results of the iteration are undefined under these circumstances... "
You don't know that it is multithreaded. It could very well be single threaded
You don't know it is single-threaded either, that's all I said. I apologized for the fact that I may have wanted to imply that the problem was not due to the iter./modif. thing. This was confusing. But it still true that even it this error is fixed, ConcurrentModificationException may still occur, depending on how taskCollection is accessed. See docs.oracle.com/javase/1.4.2/docs/api/java/util/…, the first reason given for such an exception is a race condition, and the iteration/modification in single-threaded environment come in second position only.
0

Re-formatted Truong's answer from comments:

ListIterator<Task> iterator = this.taskCollection.listIterator();
boolean marker = false;

if(taskCollection.isEmpty())
  this.taskCollection.add(t);
else {
  while (iterator.hasNext()) {
    if(iterator.next().isOverlapped(t) == false)
      marker = true;
  }
  if (marker == true)
    taskCollection.add(t);
}

Comments

0

Maintain two iterators.

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Example_v3 {

     public static void main(String[] args) {
          List<String> list = new ArrayList<String>();

          // Insert some sample values.
          list.add("Value1");
          list.add("Value2");
          list.add("Value3");

          // Get two iterators.
          Iterator<String> ite = list.iterator();
          Iterator<String> ite2 = list.iterator();

          // Point to the first object of the list and then, remove it.
          ite.next();
          ite.remove();

          /* The second iterator tries to remove the first object as well. The object does
           * not exist and thus, a ConcurrentModificationException is thrown. */
          ite2.next();
          ite2.remove();
     }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.