📄 graph2.java
字号:
package examples.inner;
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();
/** Add a node to the graph
* @param x the x coordinate of the node
* @param y the y coordinate of the node
*/
public void addNode( int x, int y ) {
// the use of "this." is not required here
this.new Node( x, y );
}
/** Get the object as a string
* @return the object as a string
*/
public String toString() {
StringBuffer sb = new StringBuffer( "[ " );
Enumeration e = nodeList.elements();
while ( e.hasMoreElements() ) {
sb.append( e.nextElement().toString()
+ " " );
}
sb.append( "]" );
return sb.toString();
}
/** Test method
* @param args not used
*/
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 );
}
/** The class representing nodes within the graph
*/
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 );
}
}
/** Determine the key value for a node
* @return the key as a String
*/
public Object key() {
return x + "," + y;
}
/** Get the object as a string
* @return the object as a string
*/
public String toString() {
return "(" + x + "," + y + ")";
}
} // end of Node class
} // end of Graph2 class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -