mayeye1989 2015-12-10 23:22 采纳率: 54.5%
浏览 1404
已采纳

高分求解JAVA疑难,希望能得到解答

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确实不一样的,为什么呢?

  • 写回答

2条回答 默认 最新

  • EAison 2015-12-11 00:26
    关注

    别的我们不谈 你这findflight方法就有问题 你集合查询永远都是查第一次而且查询的是第一个元素 建议你初学不要省略括号 if else括号内容要分清!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码