我已经使用GraphStream库编写了一段时间,我想创建一个随机图。以下是我的进展:
我的代码如下:
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");
我需要使节点变量名称动态化。这就是我被困住的地方。
答案 0 :(得分:0)
我想我解决了:
Node A[] = new Node[numNodes];
for (int i = 1; i<=numNodes-1; i++) {
A[i] = graph.addNode(String.valueOf(i));
}