import java.util.Hashtable; import java.util.Enumeration; /** Class representing an undirected graph composed * of nodes. The node class is a top-level class * nested within the Graph2 class. */ public class Graph2 { private Hashtable nodeList = new Hashtable(); public void addNode( int x, int y ) { // the use of "this." is not required here this.new Node( x, y ); } public String toString() { StringBuffer sb = new StringBuffer( "[ " ); Enumeration e = nodeList.elements(); while ( e.hasMoreElements() ) { sb.append( e.nextElement().toString() + " " ); } sb.append( "]" ); return sb.toString(); } public static void main( String[] args ) { System.out.println( "creating the graph" ); Graph2 g = new Graph2(); System.out.println( "adding nodes" ); g.addNode( 4, 5 ); g.addNode( -6, 11 ); System.out.println( g ); } private class Node { private int x, y; public Node( int x, int y ) { this.x = x; this.y = y; // the use of "Graph2.this." is not // required here if ( ! Graph2.this.nodeList .containsKey( key() ) ) { nodeList.put( key(), this ); } } public Object key() { return x + "," + y; } public String toString() { return "(" + x + "," + y + ")"; } } // end of Node class }