我有一个图声明为
Graph<MyBase, DefaultEdge> myGraph = new DefaultGraph<MyBase, DefaultEdge>();
我希望能够保留图形和边线,并用子类型(例如
)替换顶点的基类类型。for (Mybase mybase : myGraph.vertexSet()) {
mybase = mySubType;
}
但是我不认为这可行,顶点类型pf仍然是MyBase。这是我可以用jGraph实现的吗?以下似乎无效
public static <Object, DefaultEdge> void replaceVertex(
Graph<MyBase, org.jgrapht.graph.DefaultEdge> graph, MyBase vertex,
MyBase replace) {
graph.addVertex(replace);
for (org.jgrapht.graph.DefaultEdge edge : graph.outgoingEdgesOf(vertex))
graph.addEdge(replace, graph.getEdgeTarget(edge), edge);
for (org.jgrapht.graph.DefaultEdge edge : graph.incomingEdgesOf(vertex))
graph.addEdge(graph.getEdgeSource(edge), replace, edge);
graph.removeVertex(vertex);
}
当replace
是基类类型时,这很好用,当replace
是子类型时,则顶点丢失。我该如何工作?
答案 0 :(得分:0)
Graph<Number,DefaultEdge> g = new DefaultGraph<>(DefaultEdge.class);
Double mydouble=new Double(5.0);
Integer myInteger=new Integer(4);
g.add(myDouble);
g.add(myInteger);
这很好用:这里的顶点类型是Integer和Double的超类型。