2

Is there any way to abbreviate the print() and toString() into one function in a Java linked list function or is there any explanation as to why someone would format this way?

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

@Override
public String toString() {
    String display = "";
    LinkedList current = this;
    while (current != null) {
        display += new Integer(current.head).toString() + ",";
        current = current.tail;
    }
    display = display.substring(0, display.length()-1);
    return display;
}
6
  • 1
    Why keeps everone calling Java methods functions? Is that what they're teached at schools nowadays? Commented Jul 15, 2010 at 18:50
  • just dont do New Integer, use Integer.valueOf Commented Jul 15, 2010 at 18:50
  • @BalusC This is the first time I've sensed negativity on stackoverflow and it bothers me. No, I'm self-learning programming and I just came from C++ so it's been hard on transition Commented Jul 15, 2010 at 19:05
  • Nothing personal :) I just wondered because lately I started to see it more than often daily. It would maybe be a new trend or so ;) Commented Jul 15, 2010 at 19:09
  • Some of us work in multiple languages, some of which come with different nomenclature. When I visit a Delphi shop, I sometimes annoy the other programmers by using Java naming conventions for field (or is it variable?) names. For 25 years, my procedures were called SUBROUTINE (in capital letters!) So I don't worry too much about political correctness (from others) so long as people get the general idea. Commented Jul 15, 2010 at 19:43

5 Answers 5

4

I would use StringBuilder, because it's more memery efficient :

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

@Override
public String toString() {
    StringBuilder display = new StringBuilder();
    for(Integer current: this) {
        display.append(current).append(',');
    }
    return display.toString().substring(0, display.length()-1);
}
Sign up to request clarification or add additional context in comments.

Comments

1

No. There is no way to do that in Java 6. Future Javas? Don't know. :-)

The way you are doing it, it's pretty much the more concise way to achieve it.

Comments

1

The code you've shown is very amateurishly written. Concatenating into a String is just the main red flag.

If you're not too concerned about performance, you could stringify the whole list in one line of code:

return Arrays.deepToString(toArray());

Of course this depends on elements of the list having sensible toString() methods of their own.

Comments

1

You could add a toString() to the class representing a node of your linked list:

class Node<T> {
    T value;
    Node<T> next;

    public String toString() {
        return value + (next != null ? ", " + next : "");
    }
}

And then implement the toString() of your linked list as follows:

public class LinkedList<T> {
    Node<T> head;

    public String toString() { 
        return "[" + (head != null ? head : "") + "]";
    }
}

This way it will print the nodes recursively.

The print() function in turn can be replaced by just printing this since System.out.println(object) under the hoods returns String.valueOf(object) which in turn under the hoods returns object != null ? object.toString() : "null".

public void print() {
    System.out.println(this);
}

Comments

0

The reason it's done like this is that if you want to write a number of child classes which report themselves as Strings in different ways, you only have to override the toString() function on each, not the toString() and the print() function. If you want print() and toString() to have different functionality there is no reason why you can't override just print() to make it not call toString().

Comments

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.