Java 将传入的字符串转换为DirectedGraph的顶点
在创建顶点和在有向图中使用顶点时遇到一些问题 我需要关注两个类:Node.java和Digraph.javaJava 将传入的字符串转换为DirectedGraph的顶点,java,parsing,data-structures,directed-graph,Java,Parsing,Data Structures,Directed Graph,在创建顶点和在有向图中使用顶点时遇到一些问题 我需要关注两个类:Node.java和Digraph.java public class Node { private final int vertex; private HashSet<Node> outgoingEdges; public Node(int index) { this.index = index; } protected void addOutgoing(Node start)
public class Node {
private final int vertex;
private HashSet<Node> outgoingEdges;
public Node(int index) {
this.index = index;
}
protected void addOutgoing(Node start) {
outgoingEdges.add(start);
}
public class Digraph {
private Set<Node> vertices;
public Digraph(String str) {
String eliminatePrefix = str.replaceAll("a", "");
for (int i = 0; i < eliminatePrefix.length; i++) {
// Not sure how to proceed
//
}
protected void add(Node a, Node b) {
a.addOutgoing(b);
}
公共类节点{
私有最终整数顶点;
私人散列支出;
公共节点(int索引){
这个指数=指数;
}
受保护的void addouting(节点开始){
支出。添加(开始);
}
公共类有向图{
私有集顶点;
公共有向图(字符串str){
字符串eliminatePrefix=str.replaceAll(“a”,“a”);
对于(int i=0;i
传入的字符串类似于[a1,a2][a16,a10]。。。。
a1和a2是顶点。配对意味着有一条从a1到a2的定向路径。我知道字符串需要解析和转换,一旦它们正确格式化,就会调用addEdge方法。我只是想找出这个构造函数。可能不是最干净的方法,但它可以工作
String s = "[a1, a2][a3, a4][a5, a6]";
s = s.replaceAll("[a\\s\\[]", ""); // remove " " "[" and ","
String[] edges = s.split("]"); // split the string into substrings on ]
for (String edge : edges) {
String[] points = edge.split(",");
int[] p = new int[2];
p[0] = Integer.parseInt(points[0]);
p[1] = Integer.parseInt(points[1]);
// Construct your edge here
System.out.println("Edge from: " + p[0] + " to " + p[1]);
}