0

我从以下代码中得到一个 java.util.ConcurrentModificationException ,我可以找到原因。我可以成功地从 csv 文件中读取数据并制作一个名为 course list 的数组列表。然后我需要将我分类到一个数组列表中,其中每个单元格都包含一个相同课程的数组列表(具有相似名称的课程)。但是当我运行它时会生成 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();
    }


}

这是我的 CourefileSorter 类:

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




}
4

2 回答 2

0

1. 你得到 ConcurrentModificationException 因为:

dissimilarCourseList.add(currentCourse);

courseItr = 不同的CourseItr;

2. 当你有数组列表时使用迭代器不是一个好主意。

于 2013-04-04T22:31:29.787 回答
0

将它们分类为不同的数据结构会容易得多。我看到它course有一个getCourseName()方法,我假设它会返回一个String对象。尝试使用 aMap<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();This 从基础集合中删除course对象,这意味着您计划执行此操作的方式courseList将从您的CourseLister对象中清空。

于 2013-04-04T22:32:12.637 回答