Java Data Structure
Java Data Structure
John Hamer
Abstract
We present a simple, general-purpose tool for visu-
alising Java data structures. The tool uses Java re-
flection and an open-source graph drawing program
to produce text-book quality depictions of arbitrary digraph NoName {
n1 [label="LinkedList|{size: 3}",
Java objects. shape=record];
The tool offers certain pedagogical advantages over n1 -> n2 [label="header"];
other “heavy-weight” visualisation systems. Its sim- n2 [label="Entry|{null}",shape=record];
plicity and generality means that students are able n2 -> n3 [label="next"];
to visualise their own data structures, rather than n3 [label="Entry|{A}",shape=record];
passively observing a limited range of “correct” visu- n3 -> n4 [label="next"];
alisations prepared in advance by the lecturer. n4 [label="Entry|{B}",shape=record];
The tool supports an active, exploratory style of n4 -> n5 [label="next"];
learning, and is ideally suited for use in CS1-level n5 [label="Entry|{C}",shape=record];
n5 -> n2 [label="next"];
courses that introduce Java references and arrays, as n5 -> n4 [label="previous"];
well as a range of CS2-level data structure material. n4 -> n3 [label="previous"];
Initial classroom results are encouraging. n3 -> n2 [label="previous"];
n2 -> n5 [label="previous"];
}
1 Origins
Our visualisation tool was originally conceived as Figure 1: A graph specified in GraphViz notation
an aid to students learning about linked lists. It
started one evening, when the author wrote a “pretty-
printer” for linked lists that generated a description
suitable for input into the GraphViz graph drawing
utility (see figure 1). GraphViz notation is quite sim-
ple, essentially requiring just the graph edges to be
listed. Node and edge annotations are given in square
brackets. The graph generated from this description
is shown in figure 2. A variety of output formats are
supported by GraphViz, including the widely under-
stood “PNG” (for on-screen viewing) and encapsu-
lated postscript (for printed documents).
The linked list “pretty-printer” turned out to be LinkedList size: 3
of limited use, however. Students could only generate
pictures of “correct” linked lists from code supplied to header
jects.
1 2 3
String x = "Hello";
String y = x; 0 1 2
drawGraph( new Object[]{ x, y } );
a b c
The alternative to reference assignment is a copy
assignment, exemplified by the code fragment be-
low (fig. 4(b)).
Figure 5: Object and primitive arrays
String x = "Hello";
String y = new String(x);
drawGraph( new Object[]{ x, y } );
0 1 2 3
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 0 1
Figure 6: Java 2d arrays
(a) Reference assign- (b) Value assignment Static fields are not part of any object This
ment
fact is reinforced in every visualisation. Static
fields are not displayed except in the (unusual)
case of an explicit reference to a static object.
We have found this an especially helpful property
0 1 of the visualisation tool, as students quickly re-
frain from adding spurious static modifiers to
offset: 0 offset: 0 fields when they find nothing appears in their
java.lang.String count: 5 java.lang.String count: 5 graph!
hash: 0 hash: 0
We have also found that the visualiser helps
value value discourage students from declaring unnecessary
fields, since these add clutter to all their graphs.
H e l l o
4 Customising the drawing context
(c) Shallow copying
A drawing context is used to control the appearance
of graph nodes and edges, to elide classes and fields,
Figure 4: Reference and value assignment and to control the format and naming of output files.
Alternatively, an “empty” context can be used A default drawing context can be configured to
to reveal the extent to which copying of strings give reasonable defaults, such as ignoring inaccessible
actually occurs (fig. 4(c)). Examples such as this fields or treating system classes as primitive types.
are useful for introducing the concept of “shal- Configuration of the drawing context is ongoing.
low” versus “deep” copying. The operations currently supported include:
7 Availability
The visualisation tool, comprising a single Java source
file, can be requested by emailing the author.
The GraphViz utility can be downloaded for free
from http:// www.graphviz.org.
References
Fröhlich, M. & Werner, M. (1994), Demonstration
of the interactive graph visualization system
daVinci, in R. Tamassia & I. Tollis, eds,
‘Proceedings of DIMACS Workshop on Graph
Drawing ‘94’, Vol. 894, Springer Verlag, Prince-
ton (USA).
URL: ftp:// ftp.Uni-Bremen.DE/ pub/
graphics/ daVinci/ papers/ lncs.ps.gz
Gestwicki, P. & Jayaraman, B. (2002), Interactive vi-
sualization of Java programs, in ‘Symposia on
Human Centric Computing Languages and Envi-
ronments (HCC’02)’, IEEE, Arlington, Virginia,
USA, pp. 226–235.
Google.com (2003).
URL: http:// directory.google.com/ Top/
Science/ Math/ Combinatorics/ Software/
Graph˙ Drawing/