-2

I'm creating a toString() function that should return a String created by concatenating each Nodes toString(). A single space: ‘ ’ should be inserted between each Nodes toString(). No trailing space should be inserted.

For example, if the list contained 3 Node objects, the toString() return value would be [1 2 3], but not [123] or [1 2 3 ].

This is what I've tried so far and but it does not put a space between the elements (I get [01] when I need [0 1] for example):

public String toString() {
        
        String str = "";
        
        Node temp = head;
        
        while (temp != null) {
        
                str += temp.getString() + "";
                temp = temp.next;
        }
        
        return str;

    }

Here is the doubly linked list class I have with the add method:

public class DSEList {
    
    public Node head;
    private Node tail;

    public DSEList() {
        head = null;
        tail = null;
        
    }
    
    public DSEList(Node head_) {
        
        head = head_;
    }

public boolean add(String obj) {
        
            Node newNode = new Node(null, null, new String(obj));
            
            Boolean success = false;
        
            if(head == null) {  
                head = tail = newNode;  
                head.prev = null;  
                tail.next = null;
                success = true;
            } 

            else {  
                
                tail.next = newNode;  
                newNode.prev = tail;  
                tail = newNode;  
                tail.next = null;
                success = true;
            }  
            
            return success;

        }  
    

Here is the node class I have where the getString() comes from:

public class Node {

    public Node next;
    public Node prev;
    
    private String t;

    public Node(Node next, Node prev, String token) {
        this.next = next;
        this.prev = prev;
        this.t = token;
    }

    public String getString() {
        return t;
    }

    @Override
    public boolean equals(Object other) {
        if (this == other)
            return true;
        if (other == null)
            return false;
        if (!(other instanceof Node))
            return false;

        return t.equals(((Node)other).getString());
    }

    @Override
    public int hashCode() {
        if ( t == null )
            return 0;
        return t.hashCode();
    }

}

Here is an example of how it would work:

DSEList l = new DSEList();
l.add(new String(""+0));
l.toString() //this should return [0]
l.add(new String(""+1));
l.toString() //this should return [0 1]
l.add(new String(""+2));
l.toString() //this should return [0 1 2]

Thank you!

3
  • 1
    Do you expect str += temp.getString() + ""; to add the space? Why or why not? Can you fix it? Then you'll only need to remove (or not add) the trailing space. Commented Jun 8, 2023 at 14:06
  • Use a StringBuilder and append the space before each item iff it is non-empty. Commented Jun 8, 2023 at 15:39
  • 2
    There is no sense in doing something like new String(""+0). The expression "" + 0 does already produce a String, which is by the way exactly the same as "0", passing this string into a new String(…) is obsolete. So just use l.add("0");, l.add("1");, and l.add("2"); • The simplest approach is to use StringJoiner, e.g. StringJoiner j = new StringJoiner(" "); for(Node node = head; node != null; node = node.next) { j.add(node.getString()); } return j.toString(); Or, to also get the square brackets replace new StringJoiner(" ") with new StringJoiner(" ", "[", "]") Commented Jun 8, 2023 at 17:08

1 Answer 1

0

Use a StringJoiner or a StringBuilder rather than repeatedly appending strings in a loop. If using a StringBuilder, prepend the space if and only if the StringBuilder is non-empty.

Example with StringJoiner:

@Override
public String toString() {
    StringJoiner joiner = new StringJoiner(" ");
    
    Node node = head;
    while (node != null) {
        joiner.append(node.getString());
        node = node.next;
    }
    
    return joiner.toString();
}

Example with StringBuilder:

@Override
public String toString() {
    StringBuilder sb = new StringBuilder();
    
    Node node = head;
    while (node != null) {
        if (!sb.isEmpty()) {
            sb.append(' ');
        }
        sb.append(node.getString());
        node = node.next;
    }
    
    return sb.toString();
}

Note: temp is a terrible variable name for anything other than temp files and the intermediate value used in a swap.

Sign up to request clarification or add additional context in comments.

2 Comments

Or StringJoiner, which uses a StringBuilder and does the separator logic for you: StringJoiner j = new StringJoiner(" "); for(Node node = head; node != null; node = node.next) { j.add(node.getString()); } return j.toString();
@Holger Thanks. I often use Collectors.joining but I always seem to forget that StringJoiner exists as a separate standalone utility.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.