public Flight findFlight(String num) {
for (int i=0;i<flightList.size();i++)
if (flightList.get(i).getFlightnum().equals(num)) {
return flightList.get(i);
}
return null;
}
/*
* public boolean addFlight - passing in a parameter of type flight, adds
* the parameter into the arraylist. allFlights arraylist. If so, returns
* that flight object from the arraylist, otherwise, it should return null.
*/
public boolean addFlight(Flight flight1) {
Flight findFlightStatus = this.findFlight(flight1.getFlightnum());
if (findFlightStatus == null) {
flightList.add(flight1);
return true;
} else
System.out.println(
"Cannot be added to the schedule, because there is another flight with the same flight number.");
return false;
}
这两段代码,当我在main方法使用addFlight在flightlist里增加元素的时候,第一个元素总是能正确添加,但是第二个元素开始,便不断提示"Cannot be added to the schedule, because there is another flight with the same flight number."这一句.即便我的flightnum确实不一样的,为什么呢?