-3

when i try to remove element there is an error, i have uploaded the error in the image section. any help to fix? of any other way to get the code fixed and working smoothly and it it works while deleting the middle element not the first or second duplication issue ,fixed using iterator. hatsooff to stack overflow . all good

import java.util.*;

public class EarthquakeList {
private ArrayList<EarthquakeNode> records;

public EarthquakeList() {
   records = new ArrayList<EarthquakeNode>();

}

public boolean isempty() {
      return true;
}


public void add(String EarthquakeLo,String EarthquakeDt, double EarthquakeSgth, int EarthquakeDu) {
   EarthquakeNode en = new EarthquakeNode(EarthquakeLo, EarthquakeDt, EarthquakeSgth, EarthquakeDu);
   records.add(en);
}


public boolean remove(String EarthquakeLo,String EarthquakeDt) {
   boolean flag= false;
   for(EarthquakeNode earthquake: records)
   {
       if(earthquake.getLocation().equalsIgnoreCase(EarthquakeLo))
       {
           if(earthquake.getDate().equals(EarthquakeDt))
           {
               records.remove(earthquake);
               flag=true;

           }
       }
   }
   return flag;
 }


 public EarthquakeNode search(String EarthquakeLo,String EarthquakeDt) {
   EarthquakeNode node= null;
   for(EarthquakeNode earthquake: records)
   {
       if(earthquake.getLocation().equalsIgnoreCase(EarthquakeLo))
       {
           if(earthquake.getDate().equals(EarthquakeDt))
           {

               node=earthquake;
           }
       }
   }
   return node;
}


public boolean clear() {

   int count=records.size();
   for(EarthquakeNode earthquake: records)
   {
       count=count-1;
       records.remove(earthquake);



   }
   if(count==0)
   {
   System.out.println("already empty");
   return false;}

   else 
   System.out.println("every thing is removed");
   return true;
}


public boolean isempty(String EarthquakeLo,String EarthquakeDt) {
   boolean flag= false;
   for(EarthquakeNode earthquake: records)
   {
       if(earthquake.getLocation().equalsIgnoreCase(EarthquakeLo))
       {
           if(earthquake.getDate().equals(EarthquakeDt))
           {
               flag=true;
           }
       }
   }
   return flag;
}


public void print() {
   for(EarthquakeNode earthquake: records)
   {
       System.out.println( earthquake.getLocation() +" - "
              + earthquake.getDate()+ " - "
               + earthquake.getStrength() + " on rector scale"+
               "-" + earthquake.getDuration() + "mins");
   }

}


}
Adnan Ali
  • 3
  • 3

1 Answers1

0

You need to use an Iterator to safely remove from a collection while iterating over it.

public boolean remove(String earthquakeLo, String earthquakeDt) {
   boolean flag= false;
   Iterator<EarthquakeNode> iterator = records.iterator();
   while(iterator.hasNext()) {
      EarthquakeNode earthquake = iterator.next();
      if(earthquake.getLocation().equalsIgnoreCase(earthquakeLo)) {
           if(earthquake.getDate().equals(earthquakeDt)) {
              iterator.remove();
              flag=true;
           }
      }
   }
   return flag;
 }
Glim
  • 361
  • 1
  • 10
  • works perfect with 3 elements.. if i add 4th element the same error occur – Adnan Ali Apr 18 '18 at 19:47
  • when i add the 4th element :( true -- Removing a specific record -- node to be deleted location : EarthquakeNode@70dea4e AUSTRALIA - 10/4/2002 - 2.0 on rector scale-5mins GERMANY - 01/8/2016 - 6.0 on rector scale-10mins AUS - 11/4/2020 - 2.0 on rector scale-5mins Exception in thread "main" java.util.ConcurrentModificationException at java.util.ArrayList$Itr.checkForComodification(Unknown Source) at java.util.ArrayList$Itr.next(Unknown Source) at EarthquakeList.clear(EarthquakeList.java:61) at TestClass.main(TestClass.java:17) – Adnan Ali Apr 18 '18 at 19:49
  • the same ConcurrentModificationError? – Glim Apr 18 '18 at 19:50
  • yeah for clear method ... trying to fix java.util.ConcurrentModificationException – Adnan Ali Apr 18 '18 at 19:53
  • using Iterator for removing elements you shouldnt have the ConcurrentModificationException problems... very strange... – Glim Apr 18 '18 at 19:55
  • works perfect.... changed the clear method – Adnan Ali Apr 18 '18 at 19:55
  • public boolean clear() { int count=records.size(); Iterator iterator = records.iterator(); while(iterator.hasNext()) { EarthquakeNode earthquake = iterator.next(); count=count-1; iterator.remove(); } if(count==0) { System.out.println("already empty"); return false;} else System.out.println("every thing is removed"); records=null; return true; } – Adnan Ali Apr 18 '18 at 19:56