【发布时间】:2012-03-19 23:08:29
【问题描述】:
员工等级:
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,但是老师是这样开始并告诉我们这样开始的。
【问题讨论】:
-
请发布一些实际编译的代码,并确保包含调用部分。使用调试器自己单步调试代码,应该很容易找出发生了什么。遵守 Java 代码约定,并知道何时必须对变量执行
new。通常,例如,搜索方法不应包含任何新的 Node 或 Employee 实例。 -
您应该提交更完整的代码:用于节点和列表。否则,www.geekviewpoint.com 有一个易于遵循的linkedList 实现。
-
我编辑了帖子,除了测试课都发过了。
标签: java class linked-list