双链表不显示我正在搜索的确切值
Doubly LinkedList doesn't show the exact value I'm searching for
我正在使用 searchByName 方法在我的 Doubly LinkedList 中搜索节点的准确值。
即使我传递了 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");
}
Link 要点:https://gist.github.com/30b27d3612f95fc2ced99f50c4f23c14
您在更改节点时没有更新名字和姓氏。
另外,用等号比较字符串,而不是==。
你的方法有很多错误 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);
}
}
我正在使用 searchByName 方法在我的 Doubly LinkedList 中搜索节点的准确值。 即使我传递了 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");
}
Link 要点:https://gist.github.com/30b27d3612f95fc2ced99f50c4f23c14
您在更改节点时没有更新名字和姓氏。
另外,用等号比较字符串,而不是==。
你的方法有很多错误 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);
}
}