Java Reference
In-Depth Information
Node
Arc
children
<<read only>> name : String
<<read only>> name : String
0 .. n
toString() : String
toString(indent : int) : String
1
destination
Figure 4.10 Class diagram for tree representation
client :
root : Node
n1 : Node
l1 : Node
l2 : Node
toString()
toString(0)
toString(1)
toString(2 )
toString(2)
Figure 4.11 Sequence diagram for Node.toString() methods
concatenation makes it necessary to create a new object, which is highly
inefficient. A better way of building a concatenation of strings is to use the
StringBuffer class and its append() method (JDC 1998).
The implementation of the toString() method for class Node is a recursive
method that prints all the nodes of a sub-tree; nodes are printed indented
according to their level in the tree. To keep track of the indentation level
a String argument is used. At each recursive call the indent argument is
concatenated with the constant string indentStep in order to increase the
indentation level. The recursion termination condition is met when the
current node is a leaf.
We provide a toString() method that conforms to the one defined in the
base class Object . This method has no argument and invokes the recursive
toString() method passing an empty string as argument.
public String toString(){ return toString(""); }
private static final String indentStep # " ";
 
Search WWH ::




Custom Search