gpt4 book ai didi

java.util.ConcurrentModificationException 尝试使用迭代器时出错

转载 作者:太空宇宙 更新时间:2023-11-04 10:08:38 24 4
gpt4 key购买 nike

我正在编写一个程序,在其中创建一个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

这里的错误在哪里?

更新:这是完整的代码:

import java.util.*; 
import java.io.*;

class Person
{
Integer age;
String name;
String seatNumber;
Integer fare;
int pnr;
Person()
{
try
{
BufferedReader b = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the name of the passenger");
name = b.readLine();
System.out.println("Enter the age of the passenger");
age = Integer.parseInt(b.readLine());
System.out.println("Enter the Seat Number you want");
seatNumber = b.readLine();
pnr = (int)(Math.random()*100000000);
System.out.println("PNR number of the passenger is : "+pnr);
}
catch(Exception e)
{
System.out.println("");
}
}
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";
}
}
class EconomyPassenger extends Person
{

}
class BusinessPassenger extends Person
{

}
class Crew extends Person
{

}
public class Airline
{
public static void main(String[] args)
{
ArrayList<Person> flightAttendants = new ArrayList<Person>();
Iterator<Person> itr = flightAttendants.iterator();
while(true)
{
try
{
System.out.println("Welcome to Indigo!!!");
BufferedReader b = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter your Choice");
System.out.println("1.Book Tickets");
System.out.println("2.Check Reservation");
System.out.println("3.Update Tickets");
System.out.println("4.Exit");
int choice=Integer.parseInt(b.readLine());
if(choice<0 || choice>4)
{
throw new InvalidChoiceException("Enter a valid choice between 1 and 4");
}
switch(choice)
{
case 1: System.out.println("\n\n1.Economy*******2.Business*******3.Crew Login*******4.Exit");
// BufferedReader c = new BufferedReader(new InputStreamReader(System.in));
int c = Integer.parseInt(b.readLine());
if(c==1)
{
flightAttendants.add(new EconomyPassenger());
}
else if(c==2)
{
flightAttendants.add(new BusinessPassenger());
}
else if(c==3)
{
flightAttendants.add(new Crew());
}
else if(c==4)
{
break;
}
break;
case 2: // System.out.println("Enter your PNR Number : ");
// int p = Integer.parseInt(b.readLine());
// System.out.println(p);
while(itr.hasNext())
{
System.out.println(itr.next());
}
break;
case 3: System.out.println("case 3");break;
case 4: return;
default: System.out.println("default");
}
}
catch(InvalidChoiceException ic)
{
// System.out.println(ic);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
}
class InvalidChoiceException extends Exception
{
InvalidChoiceException()
{}
InvalidChoiceException(String msg)
{
System.out.println(msg);
}
}

最佳答案

您提供的代码应该可以正常工作,因为我已经尝试过了。除非您表明您的代码正在处理其他问题,否则我们无法进一步尝试解决。我建议你检查问题Iterators and the concurrentmodificationexception也是为了更好地理解您的代码可能在某个地方陷入其中提到的错误。

正如安德鲁所提到的,将迭代器带入您的 while 循环中,现在检查工作正常。我试过了。

关于java.util.ConcurrentModificationException 尝试使用迭代器时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52627252/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com