PS C:\Code> javac GenericNodeTester.java
GenericNodeTester.java:5: error: incompatible types: double cannot be converted to Integer
GenericNode<Integer> nodeA = new GenericNode<Integer>( 5.5 );
The 5.5 can't be put in an Integer wrapper and so will not work with GenericNode<Integer>.
Bird
The generic class GenericNode can be used with your own classes.
Say that you are a birdwatcher and wish to keep track of the different species of birds you have seen. Here is a class you might use. (A better class would have much more in it.)
public class Bird
{
String genus;
String species;
String commonName;
int sightings;
public Bird( String genus, String species, String commonName, int sightings)
{
this.genus = genus;
this.species = species;
this.commonName = commonName;
this.sightings = sightings;
}
public incSightings()
{
sightings++ ;
}
public String toString()
{
return genus + " " + species + " (" + commonName + ") seen:" + sightings;
}
}
The tester class uses GenericNode for this user-designed class:
public class GenericNodeTester
{
public static void main (String args[])
{
Bird robin = new Bird("Turdus", "migratoris", "Robin", 1 );
GenericNode<Bird> node = new GenericNode<Bird>( robin );
System.out.println("node: " + node );
node.getValue().incSightings();
System.out.println("node: " + node );
}
}
Source files for all three classes should be in the same subdirectory:
PS C:\Code> javac GenericNodeTester.java PS C:\Code> java GenericNodeTester node: Turdus migratoris (Robin) seen:1 node: Turdus migratoris (Robin) seen:2
One of the members of class GenericNode is
private GenericNode E next;
This declares a member variable next that can hold a reference to
another object of class GenericNode.
Do you suppose that a chain of GenericNodes could be formed?