显示不显示在数据结构输出中

Display not showing in data structure output

public class Link {

    public static void main(String[] args) {
        LinkList theLinkedList = new LinkList();
        theLinkedList.insertFirstRequest(3);
        theLinkedList.insertFirstRequest(6);
        theLinkedList.insertFirstRequest(10);
        theLinkedList.insertFirstRequest(20);

        theLinkedList.display();
    }

    int floorRequested;
    Link next;

    Link(int floorRequested) {
        this.floorRequested = floorRequested;
    }

    public void display() {
        System.out.println("Floor req: " + floorRequested);
    }
}

class LinkList {

    public Link firstRequest;

    LinkList() {
        firstRequest = null;
    }

    public boolean isEmpty() {
        return (firstRequest == null);
    }

    public void insertFirstRequest(int floorRequested) {
        Link newRequest = new Link(floorRequested);

        newRequest.next = firstRequest;

        firstRequest = newRequest;
    }

    public Link removeFirstRequest() {
        Link linkRef = firstRequest;

        if (!isEmpty()) {
            firstRequest = firstRequest.next;
        } else {
            System.out.println("No Requests");
        }
        return linkRef;
    }

    public void display() {
        Link theLink = firstRequest;

        while (theLink != null) {
            theLink.display();

            System.out.println("Next Request: " + theLink.next);
            theLink = theLink.next;

            System.out.println("");
        }
    }

    public Link find(int floorRequested) {
        Link theLink = firstRequest;

        if (!isEmpty()) {
            while (theLink.floorRequested != floorRequested) {
                if (theLink.next == null) {
                    return null;
                } else {
                    theLink = theLink.next;
                }
            }
        } else {
            System.out.println("No Requests");
        }
        return theLink;
    }

    public Link removeRequest(int floorRequested) {
        Link currenntRequest = firstRequest;
        Link previousRequest = firstRequest;

        while (currenntRequest.floorRequested != currenntRequest.floorRequested) {
            if (currenntRequest.next == null) {
                return null;
            } else {
                previousRequest = currenntRequest;
                currenntRequest = currenntRequest.next;
            }
        }
        if (currenntRequest == firstRequest) {
            firstRequest = firstRequest.next;
        } else {
            previousRequest.next = currenntRequest.next;
        }
        return currenntRequest;
    }

}

当我运行这个文件时,我只看到这个:

Floor req: 20
Next Request: elevator.model.Link@1db9742

Floor req: 10
Next Request: elevator.model.Link@106d69c

Floor req: 6
Next Request: elevator.model.Link@52e922

Floor req: 3
Next Request: null

可能是什么问题? 下一个请求是链表中的下一个请求(除了最后一个索引)。

您的 Link class 需要覆盖 toString() 方法。使用 + 运算符的字符串连接在 Java 中专门处理,正如您从 java.lang.String Javadoc(强调我的)中看到的那样:

The Java language provides special support for the string concatenation operator ( + ), and for conversion of other objects to strings. String concatenation is implemented through the StringBuilder(or StringBuffer) class and its append method. String conversions are implemented through the method toString, defined by Object and inherited by all classes in Java. For additional information on string concatenation and conversion, see Gosling, Joy, and Steele, The Java Language Specification.

当它们被串联时,superclass java.lang.Object 上的 toString() 被调用,这就是您获得该输出的原因。

public class Object {
    ...
    public String toString() {
        return getClass().getName() + "@" + Integer.toHexString(hashCode());
    }
    ...
}

问题是,默认情况下,如果您使用 "Next Request: " + theLink.next,JVM 会使用方法 toString 将 Link class 转换为字符串 class.

如果你想

How to use the toString method in Java?

public class Link {

    public static void main (String[] args) {
        LinkList theLinkedList = new LinkList();
        theLinkedList.insertFirstRequest(3);
        theLinkedList.insertFirstRequest(6);
        theLinkedList.insertFirstRequest(10);
        theLinkedList.insertFirstRequest(20);

        theLinkedList.display();
    }

    int floorRequested;

    Link next;

    Link(int floorRequested) {
        this.floorRequested = floorRequested;
    }

    public void display () {
        System.out.println(toString());
    }

    @Override
    public String toString () {
        return "Floor req: " + floorRequested;
    }
}

class LinkList {

    public Link firstRequest;

    LinkList() {
        firstRequest = null;
    }

    public boolean isEmpty () {
        return (firstRequest == null);
    }

    public void insertFirstRequest (int floorRequested) {
        Link newRequest = new Link(floorRequested);

        newRequest.next = firstRequest;

        firstRequest = newRequest;
    }

    public Link removeFirstRequest () {
        Link linkRef = firstRequest;

        if (!isEmpty()) {
            firstRequest = firstRequest.next;
        } else {
            System.out.println("No Requests");
        }
        return linkRef;
    }

    public void display () {
        Link theLink = firstRequest;

        while (theLink != null) {
            theLink.display();

            System.out.println("Next Request: " + theLink.next);
            theLink = theLink.next;

            System.out.println("");
        }
    }

    public Link find (int floorRequested) {
        Link theLink = firstRequest;

        if (!isEmpty()) {
            while (theLink.floorRequested != floorRequested) {
                if (theLink.next == null) {
                    return null;
                } else {
                    theLink = theLink.next;
                }
            }
        } else {
            System.out.println("No Requests");
        }
        return theLink;
    }

    public Link removeRequest (int floorRequested) {
        Link currenntRequest = firstRequest;
        Link previousRequest = firstRequest;

        while (currenntRequest.floorRequested != currenntRequest.floorRequested) {
            if (currenntRequest.next == null) {
                return null;
            } else {
                previousRequest = currenntRequest;
                currenntRequest = currenntRequest.next;
            }
        }
        if (currenntRequest == firstRequest) {
            firstRequest = firstRequest.next;
        } else {
            previousRequest.next = currenntRequest.next;
        }
        return currenntRequest;
    }

}

输出应该是这样的:

Floor req: 20
Next Request: Floor req: 10

Floor req: 10
Next Request: Floor req: 6

Floor req: 6
Next Request: Floor req: 3

Floor req: 3
Next Request: null

您只需将需要显示为字符串的任何信息放入 toString 方法即可。