我正在使用 searchByName 方法在我的雙向鏈表中搜索節點的確切值。 即使我傳遞了 LinkedList 中存在的值,它也不會顯示我想要的資料。
public void searchByName(String param) throws Exception{
Node currentNode = start;
String theFirstName = currentNode.firstName.toLowerCase();
String theLastName = currentNode.lastName.toLowerCase();
param = param.toLowerCase();
if (start == null) {
throw new Exception("List Underflow");
}else{
String id= "Student ID", ln="Last Name", fn="First Name", course="Course", section="Section", yl="Year Level";
System.out.format("%-10s\t%-10s\t%-10s\t%-5s\t%-10s\t%s", id, ln, fn, course, section, yl);
System.out.println();
while(currentNode.next != null){
if (param == theFirstName || param == theLastName) {
System.out.format("%-10s\t%-10s\t%-10s\t%-5s\t%-15s\t%d", currentNode.studentID, currentNode.lastName, currentNode.firstName, currentNode.course, currentNode.section, currentNode.yearLevel);
System.out.println();
}else{
System.out.println("Not found");
break;
}
currentNode = currentNode.next;
}
if (currentNode.next == null){
System.out.format("%-10s\t%-10s\t%-10s\t%-5s\t%-15s\t%d", currentNode.studentID, currentNode.lastName, currentNode.firstName, currentNode.course, currentNode.section, currentNode.yearLevel);
System.out.println();
}
}
我的主要功能:
public static void main(String[] args) throws Exception {
StudentRecord senators = new StudentRecord();
senators.insertEnd("110007", "Lacson", "Ping", "BSCS", "BSCS-III-A", "Active", 3);
senators.insertEnd("110008", "Angara", "Sonny", "BSCS", "BSCS-III-B", "InActive", 3);
senators.searchByName("Lacson");
}
鏈接到要點:https : //gist.github.com/30b27d3612f95fc2ced99f50c4f23c14
uj5u.com熱心網友回復:
您的方法中有很多錯誤 2 個主要錯誤:
- 字串應該與 equals 方法進行比較而不是 ==
- 你遍歷串列的演算法是錯誤的
- 始終使用您自己的例外 (LinkedListOutOfBoundsException)
- 不要在函式內部修改輸入引數
- 不必要的 else 陳述句,因為它拋出了。
- 最后一個 if 絕對沒用。
- 嘗試使用記錄器
public void searchByName(String param) throws LinkedListOutOfBoundsException {
if (null == start) {
throw new LinkedListOutOfBoundsException("List Underflow");
}
if (null == param) {
throw new IllegalArgumentException("param must not be null");
}
Node currentNode = start;
while (currentNode != null) {
if (param.equalsIgnoreCase(currentNode.firstName)
|| param.equalsIgnoreCase(currentNode.lastName)) {
break;
}
currentNode = currentNode.next;
}
if (null == currentNode) {
LOGGER.info("Not found");
} else {
LOGGER.info("Found {}", param);
}
}
uj5u.com熱心網友回復:
更改節點時,您不會更新名字和姓氏。
此外,將字串與等號進行比較,而不是 ==。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/353923.html
