Hello Members,

I am beginning to work with the JGrapht library. The following is described as the syntax for creating a weighted, directed graph:

SimpleDirectedWeightedGraph<String, DefaultWeightedEdge>  graph = new SimpleDirectedWeightedGraph<String, DefaultWeightedEdge>(DefaultWeightedEdge.class);

What is the meaning of including " DefaultWeightedEdge.class" in the above line?

I will be grateful for any replies.

Thank you!
sciprog1

DefaultWeightedEdge.class is a simple edge factory. It is a default way of making your edges. So by calling that you save yourself the trouble of having to make and define your own edge factory. Take a look in the api http://www.jgrapht.org/javadoc/
I hope this helps

Hello Mattox,

Thank you for the reply and the API reference.

I was just a little confused as to why a .class file would be included in a .java file in such an explicit manner. Any comments?

Regards,
sciprog1

Despite appearances that ian't a file. The .class operator is used with a named class to get the Class Object representing that class. So
DefaultWeightedEdge.class
returns an instance of class Class representing the DefaultWeightedEdge class.

It's passed as a parameter to a method to tell the method what kind (class) of Object you want to be created in that method. It's needed as a work-around for a limitation in Java 1,5 types that means you cannot do this:

void myMethod(T param) {
  new <T>();

but you can do this

void myMethod(Class paramClass) {
  paramclass.newInstance();

ps: more info if you're interested
http://stackoverflow.com/questions/75175/create-instance-of-generic-type-in-java

Hello James,

That was very helpful! Thank you also for the link.

Regards,
sciprog1

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.