1
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;
    }

}

When I run this file, I only see this:

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

What could be the problem? The next requests are meant to be the next request in the linked list (except for the last index).

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

2 Answers2

0

Your Link class needs to override the toString() method. String concatenation using the + operator is handled specially in Java, as you can see from the java.lang.String Javadoc (emphasis mine):

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.

When they are being concatenated, the toString() on the superclass java.lang.Object is being called, which is why you are getting that output.

public class Object {
    ...
    public String toString() {
        return getClass().getName() + "@" + Integer.toHexString(hashCode());
    }
    ...
}
mkobit
  • 43,979
  • 12
  • 156
  • 150
0

The problem is that by default if you use "Next Request: " + theLink.next the JVM is using the method toString to transform the Link class to a String class.

If you want to

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;
    }

}

And the output should be like this:

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

You have just to put in the toString method whatever information you need to show as String.

Community
  • 1
  • 1
David Ruiz
  • 96
  • 3