-2

i use this code to check the data in an arraylist and remove the similarities but i got a ConcurrentModificationException. this is the final code after solving all the problems:

public class Aaa {

    static ArrayList <String>  cmp   = new ArrayList<String>();
    static ArrayList <String>  cpr   = new ArrayList<String>();

    public static void clarify(ArrayList<String> cmp) {

        for (int i = 0; i< cmp.size(); i++){
            cpr.add("null");
        }
        java.util.Collections.copy(cpr, cmp);
        for (String s : cpr){
            int j = 0;
             Iterator<String> itr= cmp.iterator();
              while (itr.hasNext()){
               String t = itr.next();
                 if (s.equals(t)){
                     j++;
                     if (j > 1){
                    itr.remove();
                     }
                }
              }
            }
        for(String x : cmp){
            System.out.println(x);
        }
    }

    public static void main(String args[]){

        cmp.add("hamada");
        cmp.add("ramzy");
        cmp.add("morsy");
        cmp.add("attres");
        cmp.add("hamada");
        cmp.add("el nenny");
        cmp.add("hamada");
        cmp.add("abbas");

        clarify(cmp);       
    }
}
1
  • 5
    Did you Google this at all? I'm just hunting around for the best duplicate... Commented May 3, 2013 at 14:18

2 Answers 2

1

You cannot modify the same array on which you are iterating.

You are iterating on cmp and trying to modify the same. This is the cause of your exception.

 for (String s : cpr){
        for(String t : cmp){
            if (t.equals(s)){
                cmp.remove(t);
            }
        }
    }

use iterator instead

for (String s : cpr){
 Iterator<String> itr= cmp.iterator();
  while (itr.hasNext()){
   String t = itr.next();
     if (s.equals(t)){
        itr.remove();
    }
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

that solved for me the exception thing, but i also have another problem that when i run the program i found that it removed all the elements from the arrayList as it "for sure" found all the elements residing in the other arrayList but i want to remove the duplicates only
i had already found a solution for that, it's the edited code above
0

The reason why is you are removing elements from the array while iterating over it. This will cause ConcurrentModificationExceptions. If you want to remove elements while iterating, it's best to use the built in java iterator functionality and the method remove().

Iterator<String> iter = cmp.iterator();
while (iter.hasNext()){
    String t = iter.next();
    if (t.equals(s)){
        iter.remove();
    }
}

2 Comments

that solved for me the exception thing, but i also have another problem that when i run the program i found that it removed all the elements from the arrayList as it "for sure" found all the elements residing in the other arrayList but i want to remove the duplicates only
i had already found a solution for that, it's the edited code above

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.