简体   繁体   English

链接列表的等于方法不起作用

[英]Equals method of a linked list not working

I am having trouble implementing the remove method in the List class. 我在实现List类中的remove方法时遇到麻烦。 I am first writing the objects out into a file, then I am retrieving those objects and putting them inside a linked list. 我首先将对象写到文件中,然后检索这些对象并将它们放入链接列表中。 However, when I try to check for equality by going over the entire linked list I get not matches even though I know for sure that object is in there. 但是,当我尝试通过遍历整个链表来检查是否相等时,即使我确定该对象在其中,也不会匹配。 I can't even get .equals to work it seems. 我什至无法使.equals正常工作。

package ProjectOne;

public class List<T> {
    private LLNode<T> list;
    private int numberOfNodes = 0;
    private LLNode<T> location;
    private LLNode<T> previous;
    protected boolean found;
    public List() {
        list = null;
    }
    public void add(T element) {
        if (numberOfNodes == 0) {
            list = new LLNode<T>(element);
            numberOfNodes++;
        }
        else {  
        LLNode<T> newNode = new LLNode<T>(element);
        newNode.setLink(list);
        list = newNode;
        }
    }


    public void find(T target) {
        location = list;
        found = false;
        while(location !=null) {
            System.out.println(target.equals(location.getInfo()));
            if(location.getInfo().equals(target)) {
                found = true;
                return;
            }
            else {
                previous = location;
                location = location.getLink();
            }
        }
    }

    public boolean remove(T element) {
        this.find(element);
            if(found) {
                if(list == location) list = list.getLink();
                else previous.setLink(location.getLink());
            }
            return found;
    }

    public LLNode<T> getList() {
        return list;
    }



    public String toString() {
        LLNode<T> currentNode = list;
        String info = "";
        while(currentNode !=null) {
            info +=currentNode.getInfo();
            currentNode = currentNode.getLink();
        }
        return info;
    }

}

These are one of many objects I am writing out to the file 这些是我要写入文件的许多对象之一

 Patient p1 = new Patient("Alex", "1123 metropolitan", new Date("11/20/1997"));
p1.setFirstVisit(new Date("11/20/1997"));
p1.setHeight(72);
p1.setLastVisit(new Date("11/20/1997"));
p1.setWeight(200);
out.writeObject(p1);

Patient p2 = new Patient("John", "200 avenue of americas", new Date("12/20/1999"));
p2.setFirstVisit(new Date("11/11/2005"));
p2.setHeight(5);
p2.setLastVisit(new Date("11/21/2010"));
p2.setWeight(150);
out.writeObject(p2);

Patient p3 = new Patient("Sarah", "Park avenue", new Date("09/07/1960"));
p3.setFirstVisit(new Date("05/11/1977"));
p3.setHeight(75);
p3.setLastVisit(new Date("01/21/2017"));
p3.setWeight(110);
out.writeObject(p3);

Patient p4 = new Patient("Malcolm", "56street", new Date("05/28/1977"));
p4.setFirstVisit(new Date("01/11/1990"));
p4.setHeight(75);
p4.setLastVisit(new Date("8/21/2016"));
p4.setWeight(155);
out.writeObject(p4);

However, when I call System.out.println(list.remove(p1)); 但是,当我调用System.out.println(list.remove(p1)); I get the output false false false false false with the last being the return result of the method and the first four just trying to debug. 我得到输出false false false false false false,最后一个是方法的返回结果,而前四个只是尝试调试。

确保在Patient类中重写equals和hashcode方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM