fork(1) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. List<MyObject> list = new ArrayList<>();
  13. list.add(new MyObject("abc"));
  14. list.add(new MyObject("def"));
  15. list.add(new MyObject("ghi"));
  16.  
  17. try {
  18. for(int i = 0; i < list.size(); i++)
  19. {
  20. if(list.get(i).Id.compareTo("def") >= 0)
  21. {
  22. list.remove(list.get(i));
  23. }
  24. }
  25. } catch (Exception e)
  26. {
  27. System.out.println(e.getMessage());
  28. }
  29.  
  30. for(MyObject obj : list)
  31. {
  32. if(obj.Id.compareTo("def") >= 0)
  33. {
  34. list.remove(obj);
  35. }
  36. }
  37.  
  38. System.out.println(list.size());
  39. }
  40. }
  41.  
  42. class MyObject
  43. {
  44. public String Id;
  45.  
  46. public MyObject(String id) {
  47. Id = id;
  48. }
  49. }
Runtime error #stdin #stdout #stderr 0.12s 320576KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Exception in thread "main" java.util.ConcurrentModificationException
	at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:901)
	at java.util.ArrayList$Itr.next(ArrayList.java:851)
	at Ideone.main(Main.java:30)