问题描述
我从以下代码中获取java.util.ConcurrentModificationException,我可以找到原因。我可以成功地从csv文件中读取数据并创建一个名为其课程列表的arraylist。然后我需要将我的内容排序到一个数组列表中,每个单元格包含一个相同课程的arraylist(具有相似名称的课程)。
但是当我运行它时会产生ConcurrentModificationException并且我不明白为什么......
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();
}
}
这是我的CourefileSorterclass:
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;
}
}
}
推荐答案
将它们分类为不同的数据结构要容易得多。我看到 course
有一个 getCourseName()
方法,我假设它会返回一个字符串
对象。请尝试使用 Map< String,List< Course>>
。
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.
排序方法如下所示:
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
对象,这意味着您计划执行此操作的方式将清空来自 CourseLister
对象的 courseList
。
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.
这篇关于使用迭代器和删除时得到ConcurrentModificationException错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!