omething with node } With implicit genericity offers by Graph, this can be done easier:
Graph g = ... ; g.setNodeFactory( new MyNodeFactory() ); g.addNode("root"); MyNode n = g.getNode("root"); for( MyNode node : g.getEachNode() ) { // Do something with node }
Graph elements (nodes and edges) can be accessed using their identifier or their index. Each node / edge has a unique string identifier assigned when the element is created. Each element has an automatically maintained unique index between 0 and {@link #getNodeCount()} - 1 or {@link #getEdgeCount()} -1. When a new element is added, its index is getNodeCount() - 1
or getEdgeCount() - 1
. When an element is removed, the element with the biggest index takes its place. Unlike identifiers, indices can change when the graph is modified, but they are always successive. A loop of the form
for (int i = 0; i < g.getNodeCount(); i++) { Node node = g.getNode(i); // Do something with node }
will always iterate on all the nodes of
g
.