我正在编写一个程序,在其中创建一个ArrayList,并且我想使用迭代器遍历列表:ArrayList<Person> flightAttendants = new ArrayList<Person>();Iterator<Person> itr = flightAttendants.iterator();这是我尝试遍历数组列表元素的方式:我也定义了一个toString方法:while(itr.hasNext()){ System.out.println(itr.next());}public String toString(){ System.out.println("name of the passenger : "+name); System.out.println("Age of the passenger : "+age); System.out.println("Seat number of the passenger : "+seatNumber); return "\n"; }每当我尝试运行它时,它都会给我错误:java.util.ConcurrentModificationException这里的错误在哪里?
1 回答
宝慕林4294392
TA贡献2021条经验 获得超8个赞
您提供的代码应该可以正常工作,因为我已经尝试过了。除非您表明您的代码正在处理其他事情,否则我们无法进一步尝试解决。我建议你检查问题Iterators 和 concurrentmodificationexception也为了更好地理解你的代码可能在某个地方陷入了那里提到的错误。
正如 Andrew 所提到的,将迭代器带入您的 while 循环中,现在检查工作正常。我尝试过这个。
添加回答
举报
0/150
提交
取消