Java 如何检查ObservalArray中是否已经存在具有重复LocalDateTime属性的对象?
我编写了一个addAppointmentSaveButtonClick方法,该方法创建了如下对象:Java 如何检查ObservalArray中是否已经存在具有重复LocalDateTime属性的对象?,java,javafx,Java,Javafx,我编写了一个addAppointmentSaveButtonClick方法,该方法创建了如下对象: Appointment newAppointment = new Appointment(appointmentId, chosenCustomerId, appointmentType, startDateTime, endDateTime, customerName); MainScreenController.appointmentDisplayList.a
Appointment newAppointment = new Appointment(appointmentId, chosenCustomerId, appointmentType, startDateTime, endDateTime, customerName);
MainScreenController.appointmentDisplayList.add(newAppointment);
public static boolean existingAppointment(LocalDateTime ldt) {
for (Appointment app : appointmentDisplayList) {
if (app.getStart() == ldt) {
System.out.println("True");
return true;
}
}
System.out.println("False");
return false;
}
if(!existingAppointment(startDateTime)) {
AppointmentMethods.addAppointment(appointmentType, chosenCustomerId, utcStartTime, utcEndTime);
appointmentId = AppointmentMethods.getAppointment(chosenCustomerId, utcStartTime).getAppointmentId();
Appointment newAppointment = new Appointment(appointmentId, chosenCustomerId, appointmentType, startDateTime, endDateTime, customerName);
MainScreenController.appointmentDisplayList.add(newAppointment);}
然后将该对象添加到observableArrayList中,如下所示:
Appointment newAppointment = new Appointment(appointmentId, chosenCustomerId, appointmentType, startDateTime, endDateTime, customerName);
MainScreenController.appointmentDisplayList.add(newAppointment);
public static boolean existingAppointment(LocalDateTime ldt) {
for (Appointment app : appointmentDisplayList) {
if (app.getStart() == ldt) {
System.out.println("True");
return true;
}
}
System.out.println("False");
return false;
}
if(!existingAppointment(startDateTime)) {
AppointmentMethods.addAppointment(appointmentType, chosenCustomerId, utcStartTime, utcEndTime);
appointmentId = AppointmentMethods.getAppointment(chosenCustomerId, utcStartTime).getAppointmentId();
Appointment newAppointment = new Appointment(appointmentId, chosenCustomerId, appointmentType, startDateTime, endDateTime, customerName);
MainScreenController.appointmentDisplayList.add(newAppointment);}
现在,当我想创建新约会或编辑现有约会时,我想检查appointmentDisplayList数组中是否已经有一个具有相同开始时间的约会
StartDateTime是从组合框菜单中选择的LocalDateTime变量,其格式始终如下:2020-02-15 10:30:00
以30分钟为间隔,不存在毫秒或类似问题
到目前为止我所做的:
我创建了如下方法,该方法通过appointmentDisplayList进行迭代,如下所示:
Appointment newAppointment = new Appointment(appointmentId, chosenCustomerId, appointmentType, startDateTime, endDateTime, customerName);
MainScreenController.appointmentDisplayList.add(newAppointment);
public static boolean existingAppointment(LocalDateTime ldt) {
for (Appointment app : appointmentDisplayList) {
if (app.getStart() == ldt) {
System.out.println("True");
return true;
}
}
System.out.println("False");
return false;
}
if(!existingAppointment(startDateTime)) {
AppointmentMethods.addAppointment(appointmentType, chosenCustomerId, utcStartTime, utcEndTime);
appointmentId = AppointmentMethods.getAppointment(chosenCustomerId, utcStartTime).getAppointmentId();
Appointment newAppointment = new Appointment(appointmentId, chosenCustomerId, appointmentType, startDateTime, endDateTime, customerName);
MainScreenController.appointmentDisplayList.add(newAppointment);}
然后,每当单击“保存”按钮时,我都会将对象创建代码放入IF代码块中,如下所示:
Appointment newAppointment = new Appointment(appointmentId, chosenCustomerId, appointmentType, startDateTime, endDateTime, customerName);
MainScreenController.appointmentDisplayList.add(newAppointment);
public static boolean existingAppointment(LocalDateTime ldt) {
for (Appointment app : appointmentDisplayList) {
if (app.getStart() == ldt) {
System.out.println("True");
return true;
}
}
System.out.println("False");
return false;
}
if(!existingAppointment(startDateTime)) {
AppointmentMethods.addAppointment(appointmentType, chosenCustomerId, utcStartTime, utcEndTime);
appointmentId = AppointmentMethods.getAppointment(chosenCustomerId, utcStartTime).getAppointmentId();
Appointment newAppointment = new Appointment(appointmentId, chosenCustomerId, appointmentType, startDateTime, endDateTime, customerName);
MainScreenController.appointmentDisplayList.add(newAppointment);}
现在我在代码中遇到的问题是,我为我的新appointent选择的每个时间和日期,这个if块总是为false,并且重复的约会被添加到我的ArrayList中
我希望任何有经验的程序员都能帮我找出我做错了什么?提前谢谢你 对于对象,
=
测试它们是否是相同的对象。假设设置良好,.equals()
测试它们是否具有相同的值。有关设置.equals的详细讨论,请参阅
这就是为什么需要
app.getStart().equals(ldt)
来比较日期值,以查看两个对象是否表示相同的日期app.getStart()==ldt
正在检查它们是否是同一个对象,这不是您想要的。您是否尝试过app.getStart().equals(ldt)?您能否将其作为答案发布,以便我可以将其标记为已回答。我还是很难理解为什么它会像你建议的那样工作!非常感谢。