Java Reference
In-Depth Information
Self-Test Exercises
4. Would it make any difference if we changed the
Node
inner class in Display 15.7
from a private inner class to a public inner class?
5. Keeping the inner class
Node
in Display 15.7 as
private
, what difference would it
make if any of the instance variables or methods in the class
Node
had its access
modifiers changed from
private
to
public
or package access?
6. Why does the definition of the inner class
Node
in Display 15.7 not have the
accessor and mutator methods
getLink
,
setLink
, or other
get
and
set
methods
for the link fields similar to those in the class definition of
Node1
in Display 15.2?
7. Would it be legal to add the following method to the class
LinkedList2
in
Display 15.7?
public
Node startNode( )
{
return
head;
}
8. Rewrite the definition of the class
LinkedList2
in Display 15.7 so that it has data
of a type named
Entry
, which is a public inner class. Objects of type
Entry
have
two instance variables defined as follows:
private
String item;
private
int
count;
This rewritten version of
LinkedList2
will be equivalent to
LinkedList1
in that it
has the same methods doing the same things and it will hold equivalent data in its
nodes.
EXAMPLE:
A Generic Linked List
Display 15.8 shows a generic linked list with a type parameter
T
for the type of data
stored in a node. This generic linked list has the same methods, coded in basically the
same way, as our previous linked list (Display 15.7), but we have used a type parameter
for the type of data in the nodes.
Display 15.10 contains a demonstration program for our generic linked list. The
demonstration program uses the class
Entry
, defined in Display 15.9, as the type
plugged in for the type parameter
T
. Note that if you want multiple pieces of data in
each node, you simply use a class type that has multiple instance variables and plug in
this class for the type parameter
T
.