【问题标题】:Another issue with LinkedList implementation with another class另一个类的 LinkedList 实现的另一个问题
【发布时间】: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


【解决方案1】:

您的搜索失败,因为您错误地测试了字符串相等性。这一行:

if(emp.ID == id)

测试对象引用相等性。它仅适用于实习值(这超出了您的分配范围)。你也应该改变它:

if(id.equals(emp.ID))

关于您的代码的一些快速说明:

  • 您没有遵循命名的最佳做法。你的变量应该 以小写字母开头。
  • 您没有遵循属性范围界定的最佳做法。你的班 变量应该是私有的,带有适当的 getter/setter
  • 在您的搜索方法开始时,您不必要地创建 一个节点实例

    节点温度 = 新节点();

  • 您在 delete 中错误地测试字符串相等性 方法。

【讨论】:

  • mm 当我输入一个数字时仍然是同样的问题,但是当我输入一个文本时它可以工作......我现在看看问题是什么
  • mmm 另外,如果我添加了一个新员工,它就不起作用,除了最后一个之外都消失了
  • 您的添加功能看起来不错。您如何验证是否添加了值?
  • PrintAll 函数,同样搜索一个 ID 会显示找不到它,除非它是最后一个
  • 您可以将PrintAll 功能代码添加到您的问题中。但我建议您仔细查看您的代码,并确保您正在修复前面列出的错误。此外,在跟踪其中一些问题时,调试器是必不可少的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-05-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多