gpt4 book ai didi

Java 更改了 Arraylist 中的元素 - 需要保留先前的元素并修改其余元素

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

我的程序是一个火车时刻表,显示城市的到达和出发时间。在程序中,“延迟”输入会提示您输入城市名称和分钟数,以便为该城市的到达时间添加延迟(以分钟为单位)。这些信息全部存储在一个ArrayList中

这是我当前输出的链接:Train Schedule

这是我的延迟方法:

public void delay(String station, int minutes) {
// negative delay in minutes are allowed but no guarantee of the
// consistency of schedules of previous stations
boolean startDelay = false;
ListIterator<Station> sIt = schedule.listIterator();
while (sIt.hasNext()) {

// first find out the index of station
Station currStation = sIt.next();

if (!startDelay && currStation.getCity().equals(station)) {
startDelay = true;

} else {
currStation.delay(minutes);

}
}
startDelay = false;
}
<小时/>

但是,正是在这一点上,我必须将这些相同的分钟添加到列表中的其余时间,但不能在实现延迟之前将这些更改应用于城市。

例如,如果我输入“Edmonton”和“30”,输出应如下所示:

Link to what output should look like

<小时/>

这是我的主要方法:

     public static void main(String[] args) throws ParseException {
ArrayList<Station> schedule = new ArrayList<Station>();
SimpleDateFormat sdf = new SimpleDateFormat("d HH:mm");
schedule.add(new Station(null, sdf.parse("1 20:30"), "Vancouver"));
schedule.add(new Station(sdf.parse("2 06:00"), sdf.parse("2 06:35"), "Kamloops"));
schedule.add(new Station(sdf.parse("2 16:00"), sdf.parse("2 17:30"), "Jasper"));
schedule.add(new Station(sdf.parse("2 23:00"), sdf.parse("2 23:59"), "Edmonton"));
schedule.add(new Station(sdf.parse("3 08:00"), sdf.parse("3 08:25"), "Saskatoon"));
schedule.add(new Station(sdf.parse("3 20:45"), sdf.parse("3 22:30"), "Winnipeg"));
schedule.add(new Station(sdf.parse("4 05:02"), sdf.parse("4 05:42"), "Sioux Lookout"));
schedule.add(new Station(sdf.parse("4 15:35"), sdf.parse("4 16:10"), "Hornepayne"));
schedule.add(new Station(sdf.parse("5 00:18"), sdf.parse("5 00:48"), "Capreol"));
schedule.add(new Station(sdf.parse("5 09:30"), null, "Toronto"));

TrainTimeTable ttt = new TrainTimeTable(schedule);
Scanner inp = new Scanner(System.in);
String cmd = "";
System.out.println(("Timetable for a train travelling between Vancouver and Toronto").toUpperCase());
while (!cmd.equalsIgnoreCase("Quit")) {

System.out.print("Input [Quit | Delay | Show] timetable: ");
cmd = inp.next();
if (cmd.equalsIgnoreCase("Show")) {
System.out.println();
System.out.println("\t\tSHOWING TRAIN SCHEDULE: ");
ttt.displaySchedule();
} else if (cmd.equalsIgnoreCase("Delay")) {

System.out.print("Please enter the station where train is delayed: ");
String station = inp.next();

System.out.print("Please enter the delay time in minutes: ");
int minutes = inp.nextInt();
ttt.delay(station, minutes);
System.out.println();
System.out.println("\t\tSHOWING NEW SCHEDULE: ");
ttt.displaySchedule();

}
System.out.println();
}
inp.close();
}

}

最佳答案

稍作修改即可解决您的问题。当电台名称匹配时,将 startDelay 设置为 true 并添加该电台和所有后续电台的延迟。

public void delay(String station, int minutes) {
boolean startDelay = false;
ListIterator<Station> sIt = schedule.listIterator();
while (sIt.hasNext()) {
Station currStation = sIt.next();
if (!startDelay && currStation.getCity().equals(station)) {
startDelay = true;
}
if( startDelay ){
currStation.delay(minutes);
}
}
}

按照 David 的建议进行编辑:简化方法主体,

boolean startDelay = false;
for( Station currStation: schedule ){
if ...
if ...
}

关于Java 更改了 Arraylist 中的元素 - 需要保留先前的元素并修改其余元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48436236/

26 4 0