0

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...

public class CourseLister {
    private static final String DATA = "data\\data.csv";
    File file;
    ArrayList<Course> courseList ; 

    public CourseLister(String filepath) {
        file = new File(filepath);
        courseList = new ArrayList<>();
    }

    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<Course> getCourseIterator(){
        return courseList.iterator();
    }


    public ArrayList<Course> getCourseList(){
                return courseList;
    }

    public static void main(String [ ] args){

        CourseLister courseLister = new CourseLister(DATA);
        courseLister.readFromCsv();
        CourseFileSorter coursefilesoreter = new CourseFileSorter(courseLister.getCourseIterator());
        ArrayList<Course> curseList = courseLister.getCourseList();
        for (Course course : curseList) {
            System.out.println(course.getSemester());
        }
        System.out.println(curseList.size());
        coursefilesoreter.displayCategorizedList();
    }


}

here is my CourefileSorterclass:

public class CourseFileSorter {

    Iterator<Course> courseItr ;

    public CourseFileSorter(Iterator<Course> courseItr) {
        this.courseItr = courseItr;
    }

    public ArrayList<ArrayList<Course>> getSourtedLists(){

        Iterator<Course> dissimilarCourseItr = null;
        ArrayList<Course> identicalCourseList = new ArrayList<Course>();
        ArrayList<Course> dissimilarCourseList = new ArrayList<Course>();
        ArrayList<ArrayList<Course>> categorizedCourseList = new ArrayList<ArrayList<Course>>();
        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;
        }
    }




}
Sarah Jeffi
  • 53
  • 2
  • 10

2 Answers2

0

1 . You get ConcurrentModificationException because:

dissimilarCourseList.add(currentCourse);

courseItr = dissimilarCourseItr;

2 . It's not a good idea to use iterators when you have arraylists.

Vitaly
  • 2,760
  • 2
  • 19
  • 26
  • @jlordo item 1 - now changed. item 2 - the code looks urgly and unreadable, and there are exceptions like that – Vitaly Apr 04 '13 at 22:38
  • 1. Good find! 2. If you want to iterate over a `List` and possibly remove elements, then `Iterator` is the best idea. – jlordo Apr 04 '13 at 22:39
  • @jlordo 1. thanks 2. in some general code maybe; in this particular case.. you already know :) btw, consider two iterators (with removal in mind) over the same list - one will fail if another does his removal job. – Vitaly Apr 04 '13 at 22:48
  • 1
    I would upvote this if it wasn't for item (2) which is unmotivated and incorrect. – user207421 Apr 04 '13 at 23:55
0

It would be much easier to sort them into a different data structure. I see that course has a getCourseName() method, which I assume would return a String object. Try using a Map<String, List<Course>> instead.

The sorting method would look like this:

public Map<String, List<Course>> getSourtedLists(){
    Map<String, List<Course>> result = new HashMap<String, List<Course>>();
    while(courseItr.hasNext()) {
        course next = courseItr.next();
        if (!result.containsKey(next.getCourseName())) {
            result.put(next.getCourseName(), new ArrayList<Course>());
        }
        result.get(next.getCourseName()).add(next);
}

Also, you REALLY don't want to call courseItr.remove(); This removes the course object from the underlying Collection, meaning that the way you were planning to do it would empty out the courseList from your CourseLister object.

Rob Watts
  • 6,866
  • 3
  • 39
  • 58