使用迭代器和删除时得到ConcurrentModificationException错误

时间:2013-04-04 22:25:29

标签: java iterator

我从以下代码中获取java.util.ConcurrentModificationException,我可以找到原因。我可以成功地从csv文件中读取数据并创建一个名为课程列表的arraylist。然后我需要将我的内容排序到一个数组列表中,每个单元格包含一个相同课程的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;
        }
    }




}

2 个答案:

答案 0 :(得分:0)

1。你得到ConcurrentModificationException,因为:

dissimilarCourseList.add(currentCourse);

courseItr = dissimilarCourseItr;

2。当你有arraylists时,使用迭代器并不是一个好主意。

答案 1 :(得分:0)

将它们分类为不同的数据结构要容易得多。我看到coursegetCourseName()方法,我假设该方法会返回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();这会从基础Collection中删除course对象,这意味着您计划执行该操作的方式会清空courseList来自您的CourseLister对象。