Java 另一个类的LinkedList实现存在另一个问题
员工类别:Java 另一个类的LinkedList实现存在另一个问题,java,class,linked-list,Java,Class,Linked List,员工类别: public class Employee { String ID; String Fname; String Lname; String City; String Major; int GPA; public Employee() { GPA = 0; } public void ShowInfo() { JOptionPane.showMessageDialog(null, "ID: " + ID + "\nFirst Name: " + Fnam
public class Employee
{
String ID;
String Fname;
String Lname;
String City;
String Major;
int GPA;
public Employee()
{
GPA = 0;
}
public void ShowInfo()
{
JOptionPane.showMessageDialog(null, "ID: " + ID + "\nFirst Name: " + Fname + "\nLast Name:" + Lname + "\nCity: " + City + "\nMajor: " + Major + "\nGPA: " + GPA);
}
public void EnterInfo()
{
ID = JOptionPane.showInputDialog("Enter Student ID");
Fname = JOptionPane.showInputDialog("Enter Student First Name");
Lname = JOptionPane.showInputDialog("Enter Student Last Name");
City = JOptionPane.showInputDialog("Enter Student City");
Major = JOptionPane.showInputDialog("Enter Student Major");
String gpa = JOptionPane.showInputDialog("Enter Student GPA");
GPA = Integer.parseInt(gpa);
}
}
}
链接列表:
public class EmployeeList
{
Node first;
Node last;
int count;
public EmployeeList()
{
first = new Node();
first = null;
last = new Node();
last = null;
count = 0;
}
public boolean empty()
{
return first == null;
}
public void add(Employee emp)
{
Node newEmployee = new Node();
newEmployee.e = emp;
newEmployee.e.EnterInfo();
newEmployee.next=null;
if(empty())
{
first=newEmployee;
last=first;
}
else
{
last.next = newEmployee;
last = last.next;
}
count++;
}
public boolean search(String id)
{
Node temp = new Node();
Employee emp = new Employee();
temp = first;
while(temp!=null)
{
emp = temp.e;
if(id.equals(emp.ID))
{
emp.ShowInfo();
return true;
}
temp=temp.next;
}
return false;
}
public boolean delete(String id)
{
Employee emp = new Employee();
if(!empty())
{
if(first.e.ID.equals(id))
{
first=first.next;
return true;
}
else
{
Node previous = new Node();
Node temp = new Node();
previous = first;
temp = first.next;
while(temp!=null)
{
emp = temp.e;
if(id.equals(emp.ID))
{
count--;
previous.next = temp.next;
return true;
}
previous = previous.next;
temp = temp.next;
}
return false;
}
}
return false;
}
public String ALL()
{
String all = new String();
Node temp = new Node();
Employee emp = new Employee();
temp = first;
while(temp!=null)
{
emp = temp.e;
all = all + emp.ID + "-";
temp = temp.next;
}
all = all + "null";
return all;
}
}
我真的不知道这里有什么问题,如果我尝试打印它们,我会一直得到最后输入的值
节点类:
public class Node
{
Employee e = new Employee();
Node next;
}
通过搜索,我没有得到任何结果,只是找不到员工ID。
EnterInfo方法仅用于输入变量(ID、Fname…)
有什么帮助吗?谢谢
编辑:我知道这样做是错误的,我应该添加getter和setter,但老师就是这样开始的,并告诉我们以这种方式开始的。您的搜索失败,因为您错误地测试了字符串相等性。这一行:
if(emp.ID == id)
对象引用相等性测试。它只适用于实习值(这超出了您的任务范围)。你也应该改变它:
if(id.equals(emp.ID))
关于代码的一些快速注释:
- 您没有遵循命名方面的最佳实践。你的变量应该 以小写字母开头
- 您没有遵循物业范围界定的最佳实践。你们班 变量应该是私有的,带有适当的getter/setter
- 在搜索方法的开头,您不必要地创建了 节点实例 节点温度=新节点()
- 您的
方法delete
PrintAll
函数代码添加到问题中。但我建议您仔细检查代码,确保正在修复前面概述的错误。此外,调试器在跟踪这些问题时是必不可少的。请发布一些实际编译的代码,并确保包含调用部分。使用调试器自己一步一步地检查代码,这样应该很容易发现发生了什么。遵守Java代码惯例,并了解何时必须对变量执行new
。通常,例如,搜索方法不应包含任何新节点或员工实例。您应提交更完整的代码:用于节点和列表。另外,www.geekviewpoint.com有一个易于理解的linkedList实现。