문제

I'm new Java and am using jGraphT to create a SimpleDirectedWeightedGraph. I'm getting this error when trying to set weights on my edges after creating and adding them to the graph:

Exception in thread "main" java.lang.ClassCastException: ObservationsDAG$ObservationsDAGEdge cannot be cast to org.jgrapht.graph.DefaultWeightedEdge
at org.jgrapht.graph.AbstractBaseGraph.setEdgeWeight(Unknown Source)

I am assuming I need to do something in my ObservationsDAGEdge class here, but from looking at the JGraphT docs, I am stuck as to what that is. Does my edge class need weight instance variable and do I need to provide getEdgeWeight() and setEdgeWeight()?

올바른 솔루션이 없습니다

다른 팁

So, I figured this out. I first tried extending DefaultDirectedEdge, but then getEdgeTarget() stopped working - probably because I need to implement start/end vertices.

After reading the code, I tried subclassing SimpleDirectedWeightedGraph and overrode setEdgeWeight and getEdgeWeight and gave my edge class a weight instance variable.

That finally worked as sexpected.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top