-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);       
    }
}
Muhammed Refaat
  • 8,914
  • 14
  • 83
  • 118

2 Answers2

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();
    }
  }
}
NullPointerException
  • 3,732
  • 5
  • 28
  • 62
  • 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 – Muhammed Refaat May 03 '13 at 15:04
  • i had already found a solution for that, it's the edited code above – Muhammed Refaat May 03 '13 at 15:19
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();
    }
}
greedybuddha
  • 7,488
  • 3
  • 36
  • 50
  • 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 – Muhammed Refaat May 03 '13 at 15:05
  • i had already found a solution for that, it's the edited code above – Muhammed Refaat May 03 '13 at 15:17