使用迭代器并删除时出现ConcurrentModificationException错误


问题内容

我从下面的代码中获得java.util.ConcurrentModificationException,我可以找到原因。我可以成功地从csv文件读取数据,并创建一个名为课程列表的arraylist。那么我需要将其排序到一个数组列表,该数组的每个单元格都包含相同课程(名称相似的课程)的数组列表。但是当我运行它时会生成ConcurrentModificationException,我不明白为什么…

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();
    }


}

这是我的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;
        }
    }




}

问题答案:

将它们分类为不同的数据结构会容易得多。我看到course有一个getCourseName()方法,我假设它将返回一个String对象。尝试使用Map<String, List<Course>>代替。

排序方法如下所示:

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);
}

另外,你真的不想叫courseItr.remove();这消除了course从底层集合对象,这意味着你的方式正计划这样做会空出courseList从你的CourseLister对象。