基于链表队列类通过邮箱查找申请者功能故障排查求助
问题修复指南:链表队列的邮箱搜索失效问题
嘿,我帮你揪出了两个导致字符串搜索失效的核心问题,咱们一步步来解决:
1. 队列搜索方法的循环逻辑完全写反了
先看你ApplicantQueue里的search(String email)方法,循环条件搞反了!你现在的代码是:
while(current != null && current.getAppDetails().getEmail().equals(email) != false) { current = current.getLink(); }
这意思是只要当前节点的邮箱等于目标邮箱,就继续往后跳,这不就直接跳过了要找的节点吗?完全搞反了逻辑!
正确的逻辑应该是:当当前节点存在,且邮箱不等于目标时,才继续遍历。修改后的方法应该是这样:
public DegreeApp search(String email) { ListNode current = front; // 遍历直到找到匹配的邮箱,或者走到队列末尾 while(current != null && !current.getAppDetails().getEmail().equals(email)) { current = current.getLink(); } // 找到就返回详情,没找到返回null return current != null ? current.getAppDetails() : null; }
另外,为了避免空指针(比如某个申请者邮箱为null),可以再加个null判断:
while(current != null && current.getAppDetails().getEmail() != null && !current.getAppDetails().getEmail().equals(email)) { current = current.getLink(); }
2. GUI代码的两个致命错误
错误一:新建了空队列来搜索
你在GUI里新建了一个ApplicantQueue editApplicant = new ApplicantQueue();,这是个空队列啊!里面根本没有任何申请者数据,怎么可能搜得到?你应该用存储所有申请者的主队列实例来调用search方法,比如假设你的主队列叫mainApplicantQueue,就用它来搜。
错误二:用==比较字符串
Java里==比较的是对象的内存地址,不是字符串内容!你这里的两个判断:
if(editApplicant.getFront().getAppDetails().getEmail() == findEmail) { ... } if(editApplicant.getFront().getAppDetails().getEmail() == null) { ... }
即使邮箱内容匹配,==也会返回false,因为它们是不同的字符串对象。而且因为你用了空队列,getFront()大概率是null,还会触发空指针异常。
修改后的GUI代码应该是这样(记得替换成你的主队列变量名):
if(e.getSource() == btnFind) { String findEmail = txtFind.getText(); // 用存储所有申请者的主队列来搜索,而不是新建空队列! DegreeApp foundApplicant = mainApplicantQueue.search(findEmail); if(foundApplicant != null) { // 把找到的申请者信息显示到文本框(需要DegreeApp有合适的toString方法) taEditDelteDisplay.setText(foundApplicant.toString()); JOptionPane.showMessageDialog(null, "NOTICE, \nQualifications & UCAS Points have been cleared!\n you must re-enter this information!", "INFO", JOptionPane.INFORMATION_MESSAGE); } else { // 没找到的情况,清空显示并提示 taEditDelteDisplay.setText(""); JOptionPane.showMessageDialog(null, "NONE FOUND, \nEmail not found.", "ERROR", JOptionPane.ERROR_MESSAGE); } }
最后提醒
确保你的DegreeApp类有正确的toString()方法,这样taEditDelteDisplay才能正常显示申请者的信息。另外,之后做编辑功能时,直接基于foundApplicant对象修改就可以啦。
内容的提问来源于stack exchange,提问作者Sean
相关产品推荐
相关产品推荐

