title: getting ConcurrentModificationException error while using iterator and remove
body: <p>I am getting a java.util.ConcurrentModificationException from following code and I can find the reason. I could successfully read data form a csv file and make an arraylist called course list of it. then I need to sort my in to an array list that each of its cell contains an arraylist of identical courses (courses that have similar name). 
  But when I run it generates ConcurrentModificationException and I do not understand why...</p>
  
  <pre><code>public class CourseLister {
      private static final String DATA = "data\\data.csv";
      File file;
      ArrayList&lt;Course&gt; courseList ; 
  
      public CourseLister(String filepath) {
          file = new File(filepath);
          courseList = new ArrayList&lt;&gt;();
      }
  
      public void readFromCsv(){
          // in this method a Csv file is written line by line , create a new object of course with some attribute such as name , number, instructor,... and is added to courseList //}
  
          }
  
  
  
      public Iterator&lt;Course&gt; getCourseIterator(){
          return courseList.iterator();
      }
  
  
      public ArrayList&lt;Course&gt; getCourseList(){
                  return courseList;
      }
  
      public static void main(String [ ] args){
  
          CourseLister courseLister = new CourseLister(DATA);
          courseLister.readFromCsv();
          CourseFileSorter coursefilesoreter = new CourseFileSorter(courseLister.getCourseIterator());
          ArrayList&lt;Course&gt; curseList = courseLister.getCourseList();
          for (Course course : curseList) {
              System.out.println(course.getSemester());
          }
          System.out.println(curseList.size());
          coursefilesoreter.displayCategorizedList();
      }
  
  
  }
  </code></pre>
  
  <p>here is my CourefileSorterclass:</p>
  
  <pre><code>public class CourseFileSorter {
  
      Iterator&lt;Course&gt; courseItr ;
  
      public CourseFileSorter(Iterator&lt;Course&gt; courseItr) {
          this.courseItr = courseItr;
      }
  
      public ArrayList&lt;ArrayList&lt;Course&gt;&gt; getSourtedLists(){
  
          Iterator&lt;Course&gt; dissimilarCourseItr = null;
          ArrayList&lt;Course&gt; identicalCourseList = new ArrayList&lt;Course&gt;();
          ArrayList&lt;Course&gt; dissimilarCourseList = new ArrayList&lt;Course&gt;();
          ArrayList&lt;ArrayList&lt;Course&gt;&gt; categorizedCourseList = new ArrayList&lt;ArrayList&lt;Course&gt;&gt;();
          Course firstCourse = null;
          Course currentCourse ;
          if(courseItr.hasNext()){
          while(courseItr.hasNext()){
              firstCourse = courseItr.next();
              identicalCourseList.add(firstCourse);
              while(courseItr.hasNext()){
                  currentCourse = courseItr.next();
                  if(currentCourse.getCourseName().equals(firstCourse.getCourseName())){
                      identicalCourseList.add(currentCourse);
                      courseItr.remove();                 
                  }
                  else{
                      dissimilarCourseList.add(currentCourse);
                  }
              }
              dissimilarCourseItr = dissimilarCourseList.iterator();
              courseItr = dissimilarCourseItr;
              categorizedCourseList.add(identicalCourseList);         
          }
          return categorizedCourseList;
          }
          else{
              return null;
          }
      }
  
  
  
  
  }
  </code></pre>