gpt4 book ai didi

java - 用 HashMap 填充 ArrayList

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

我想用HashMap(Appointment)填充ArrayList(Appointment_List),但每次我都需要更改指定元素的值(Appt_Start_Timeendtime)。

这是代码:

for (int i = 1; i < Appt_Length; i++) {
Start_Time = End_Time;
Start_Minute = Curr_Start_Minute;
Start_Minute += My_Calendar.Get_Time_Interval();
if (Start_Minute >= 60) {
Tmp_Start_Hour += 1;
Curr_Start_Minute = 0;
} else {
Curr_Start_Minute = Start_Minute;
}
End_Time = Time.Get_Index_Time(Tmp_Start_Hour, Curr_Start_Minute);
Appointment.remove("Appt_Start_Time");
Appointment.put("Appt_Start_Time", Start_Time);
Appointment.remove("endtime");
Appointment.put("endtime", End_Time);
Appointment_List.add(Appointment);
}

但是在执行此代码后,我在 Appointment_List 中获取了约会,但所有约会都具有 Appointment.get("Appt_Start_Time")Appointment.get("endtime") 等于循环附带的最后一个值。

为什么当我添加新元素时,Appt_Start_Timeendtime 都会重置?

最佳答案

Appointment 每次迭代都是同一个 HashMap 实例。你应该这样做:

约会 = new HashMap();

//...按约定做某事...

Appointment_List.add(约会)

这样,您就可以在每次迭代时将全新的 map 添加到列表中。

关于java - 用 HashMap 填充 ArrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3413555/

25 4 0