【发布时间】:2021-12-16 17:54:12
【问题描述】:
我正在研究一个虚拟医院数据库。我有一个ArrayList,它结合了医生理论上可以预约的所有可能时间,还有一个ArrayList,它持有实际的注册预约。
Availability {
int doctorid;
String specialty;
Date date;
int order_of_appointment;
}
//////////
ArrayList<Availability> allTimes;
ArrayList<Availability> busyTimes;
我想要完成的是找到医生空闲的时间。这是 (allTimes - busyTimes) 的结果
我尝试使用allTimes.removeAll(busyTimes),但没有删除任何内容。
我确保我覆盖了 Availability 类中的 equals() 方法,但它仍然没有删除任何内容。
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Availability)) return false;
Availability that = (Availability) o;
return doctorid == that.doctorid &&
order_of_appointment == that.order_of_appointment &&
Objects.equals(specialty, that.specialty) &&
Objects.equals(date, that.date);
}
输出:
-
busyTimes = [可用性{doctorid=1,专业='内科',日期=2021-11-02,order_of_appointment=2} ]
-
所有时间 = [可用性{doctorid=1,专业='内科',日期=2021-11-02,order_of_appointment=1} , 可用性{doctorid=1, special='内科', date=2021-11-02, order_of_appointment=2} , 可用性{doctorid=1,specialty='内科', date=2021-11-02, order_of_appointment=3}]
-
我为 freeTimes 获得的输出与 allTimes 相同,尽管我希望它会删除 order_of_appointment==2 的约会。
我完全不知道是什么原因造成的。任何帮助,将不胜感激。提前致谢!
【问题讨论】:
-
你试过调试
removeAll方法吗? -
还没有,我已经关闭了我的电脑,但是我要在早上醒来时运行 jdb。我发布了这个以防我遗漏了一些明显的东西
-
很明显,你需要调试它:-)
-
我认为问题可能是由变量
date引起的。你确定他们在date上是平等的吗?无论如何,调试它。 -
您首先应该简单地测试一下您的 equla 方法是否符合您的预期。
标签: java arrays object arraylist