Java Reference
In-Depth Information
temp
,topreservethestartofthislinkedlistsothatfurthermanipulations(nodeinser-
tions, removals, updates) and searches can be performed.
Thewhileloopiteratesuntil
temp
containsthenullreference,outputtingeachnode's
name
field and assigning the reference in the current node's
next
field to
temp
.
When you run this application, it generates the following output:
node 1
node 2
node 3
Youmightdeclare thefollowing
Cell
classtorepresentasparsematrixnodefora
spreadsheet, which is known as a
cell
:
class Cell
{
int row;
int col;
Object value;
Node next;
}
When called upon to update the spreadsheet on the screen, your spreadsheet applic-
ation'srenderingcodetraversesitslinkedlistof
Cell
nodes.Foreachcell,itfirstex-
amines (
row
,
col
) to learn if the cell is visible and should be rendered. If the cell is
visible, the
instanceof
operator is used to determine
value
's type, and
value
is
then displayed. As soon as
null
is encountered, the rendering code knows that there
are no more spreadsheet elements to render.
Beforecreatingyourownlinkedlistclasstostore
Cell
instances,youshouldrealize
that doing so isn't necessary. Instead, you can leverage the Collection Framework's
LinkedList
class to store
Cell
instances (without the unnecessary
next
fields).
Although you might occasionally need to create your own node-based collections, the
moralofthisexerciseisthatyoushouldalwaysthinkaboutusingarrays,theCollections
Framework,oralegacyclasssuchas
BitSet
beforeinventingyourownAPItocollect
objects.
EXERCISES
The following exercises are designed to test your understanding of collections: