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

时间:2015-03-04 07:31:14

标签: java data-structures

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

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

2 个答案:

答案 0 :(得分:0)

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

  

Java语言为字符串提供特殊支持   连接运算符(+),以及将其他对象转换为   字符串。字符串连接是通过实现的   StringBuilder(或StringBuffer)类及其append方法。 <强>字符串   转换是通过方法toString实现的,由   对象并由Java中的所有类继承。另外   有关字符串连接和转换的信息,请参阅Gosling,Joy,   和斯蒂尔,Java语言规范。

当它们被连接时,正在调用超类toString()上的java.lang.Object,这就是你获得该输出的原因。

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

答案 1 :(得分:0)

问题是默认情况下,如果您使用&#34;下一个请求:&#34; + theLink.next JVM使用方法toString将Link类转换为String类。

如果你想

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方法中输入你需要显示为String的信息。