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確實不一樣的,為什麼呢?
別的我們不談 你這findflight方法就有問題 你集合查詢永遠都是查第一次而且查詢的是第一個元素 建議你初學不要省略括號 if else括號內容要分清!