我已经用GraphStream库编写了一点代码,现在我想创建一个随机图。我是这样做的:
- 创建图形
- 创建一个随机数生成器,在我的情况下,生成从3到1000的任何数字
- 创建节点并将其添加到图形
public class Cluster1 {
public static void main(String args[]) {
//create a graph
Graph graph = new SingleGraph("Cluster1");
// generate a random number of nodes from 3-1000
int numNodes = 3 + ((int) Math.floor(Math.random() * 998));
System.out.println("No of Nodes: " + numNodes + "n");
// add each to node to the graph
for (int i = 1; i <= numNodes; i++) {
Node (String.valueOf(i)) = graph.addNode(String.valueOf(i));
}
graph.display();
}
}
错误似乎来自这一行Node (String.valueOf(i)) = graph.addNode(String.valueOf(i));
我曾经创建节点并将它们添加到图中,如下所示:Node A = graph.addNode("A");
我需要将节点变量名设置为动态的。
我想我解决了:Node A[] = new Node[numNodes];
for (int i = 1; i<=numNodes-1; i++) {
A[i] = graph.addNode(String.valueOf(i));
}