为什么两个 LocalDate 变量之间的比较不起作用?

Why the compare between two LocalDate variables doesn't work?

我在 Main 中创建了一个 Flight 类型的 ArrayList class.Also Flight 是一个 class,它包含 LocalDate 变量、getter 和 setter。在 main 中,我创建了一个布尔标志 theDay 并将其设置为 true。当 For 循环找到与之前相同的 LocalDate 日期值时,我希望此标志更改为 false。 例如:

第一次尝试:我将值 3 放入 scanner.nextInt(); 它打印了 2019-5-3 和 "Didn't found a same date."

第二次尝试:我将值 6 放入 scanner.nextInt(); 它打印了 2019-5-6 和 "Didn't found a same date."

第三次尝试:我再次将值 3 放入 scanner.nextInt(); 它打印了 2019-5-3 和 "Didn't found a same date." 我应该收到 "A same date found".

的消息
public static void main(String[] args) {
    ArrayList<Flight> flightList = new ArrayList<Flight>();
    Scanner scanner = new Scanner(System.in);

    int counter=1;
    while (counter <= 3) {
        Flight flight = new Flight();

        System.out.println("Give the day of the departure.");

        LocalDate day = LocalDate.of(2019, Month.MAY, scanner.nextInt());
        flight.setDateOfDeparture(day);

        System.out.println(flight.getDateOfDeparture());

        boolean theDay = true; //Flag (reversed way in order to achieve the TRUE logic value).
        for (Flight flight1 : flightList) {
            System.out.println(flight1.getDateOfDeparture());

            if (flight1.getDateOfDeparture().compareTo(flight.getDateOfDeparture()) == 0) {
                theDay = false;
            }
        }

        counter++;

        if (theDay){
            System.out.println("Didn't found a same day.");
        }else
            System.out.println("A same date found");
    }
}

您永远不会在列表中添加任何实例。 根据您的期望:

  • 第一次尝试:我将值 3 放入 scanner.nextInt(); 它打印了 2019-5-3"Didn't found a same date."
  • 第二次尝试:我输入了值 6 变成 scanner.nextInt(); 它打印了 2019-5-6"Didn't found a same date."
  • 第三次尝试:我再次将值 3 放入 scanner.nextInt(); 它打印了 2019-5-3"Didn't found a same date." 我应该得到 "A same date found" 的消息。

您需要在 theDay 为真且没有匹配的出发日期时插入 flight

if (theDay){
    flightList.add(flight);
    System.out.println("Didn't found a same day.");
}else{
    System.out.println("A same date found");
}

当您准备好继续前进时,您可以使用具有正确等效实现的 Set<Flight>

如果已经有一个 "equivalent" 实例,您将永远不需要检查自己,Set 会为您完成。

您只需正确实现 equalshashCode,您的代码将看起来像:

Set<Flight> flights = new HashSet<>();

Scanner sc = new Scanner(System.in);
for(int i = 0; i < 5; ++i){
    Flight f = new Flight();
    f.setDeparture(LocalDate.of(2019, Month.MAY, sc.nextInt()));

    if(flights.add(f)){
        System.out.println("New flight added");
    } else {
        System.out.println("Flight already booked");
    }
}

sc.close();

为了给你一个想法,这里是 eclipse 为一个简单的 class

生成的方法
class Flight {
    LocalDate departure;

    public void setDeparture(LocalDate departure) {
        this.departure = departure;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result
                + ((departure == null) ? 0 : departure.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Flight other = (Flight) obj;
        if (departure == null) {
            if (other.departure != null)
                return false;
        } else if (!departure.equals(other.departure))
            return false;
        return true;
    }
}

发生这种情况是因为您没有将航班添加到 flightList,所以它始终是空的。 更改代码以在未找到日期时添加航班,在您的 if 语句中:

if (theDay){
      System.out.println("Didn't found a same day.");
      flightList.add(flight);
} else {
      System.out.println("A same date found");
}