我正在創建一個類調用學生,並希望將它們存儲在列表中我正在使用迭代器從列表中獲取元素,但我不能這樣做,因爲一個異常正在發生的事情,我不能化解這是這裏發生了異常,這將是很大的幫助,如果有人可以給我this.Thank邏輯原因,你java.util.ConcurrentModificationException有人可以解釋我的這種邏輯原因
import java.awt.List;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Vector;
import java.util.List.*;
public class Runs {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
// adding student class to list
java.util.List list = new ArrayList<Student>();
Student q = new Student("hello", 1);
Iterator<Student> l = list.iterator();
list.add(q);
// error false in this segment of the code
Student op = l.next();
String hhh = op.getnamez();
System.out.println(hhh);
System.out.println(op.getnamez());
} catch (Exception e) {
System.out.println("" + e);
}
}
public static class Student {
// student class
public String name;
private int age;
public Student(String s, int a) {
this.name = s;
this.age = a;
}
public String getnamez() {
return this.name;
}
}
}
可能的複製集合,避免ConcurrentModificationException當在循環中刪除](http://stackoverflow.com/questions/223918/iterating-through-a-collection-avoiding-concurrentmodificationexception-when-re) – Jeremy