I have instantiated an object like this:
GraphMatrixDirected<String, Integer> g
g is passed to a function like this:
floyd(g);
floyd’s signature looks like this:
public void floyd(GraphMatrixDirected<V,E> g)
Eclipse gives me an error saying:
The method floyd(GraphMatrixDirected<V,E>) in the type GraphMatrix<V,E> is not applicable for the arguments (GraphMatrixDirected<String,Integer>)
What do I need to do to fix it?
Edit1:
abstract public class GraphMatrix<V,E> extends AbstractStructure<V> implements Graph<V,E>
Edit2:
public interface Graph<V,E> extends Structure<V>
public abstract class AbstractStructure<E> implements Structure<E>
public interface Structure<E> extends Iterable<E>
/* JDT added extension of Iterable for Java 5 */
Edit 3:
Note: Funtion was changed from floyd to AllPairsShortestPath.
public void AllPairsShortestPath(GraphMatrixDirected<V,E> g)
// post: g contains edge (a,b) if there is a path from a to b
{
Iterator<V> witer = g.iterator();
while (witer.hasNext())
{
Iterator<V> uiter = g.iterator();
V w = witer.next();
while (uiter.hasNext())
{
Iterator<V> viter = g.iterator();
V u = uiter.next();
while (viter.hasNext())
{
V v = viter.next();
if (g.containsEdge(u,w) && g.containsEdge(w,v))
{
Edge<V,E> leg1 = g.getEdge(u,w);
Edge<V,E> leg2 = g.getEdge(w,v);
Integer leg1Dist = (Integer)leg1.label();
Integer leg2Dist = (Integer)leg2.label();
Integer newDist = (Integer)leg1Dist+leg2Dist;
E newDistE = (E)newDist;
if (g.containsEdge(u,v))
{
Edge<V,E> across = g.getEdge(u,v);
Integer acrossDist = (Integer)across.label();
if (newDist < acrossDist)
{
across.setLabel(newDistE);
}
}
else
{
g.addEdge(u,v,newDistE);
}
}
}
}
}
}
Perhaps you meant
floydto be a generic method?Otherwise, whatever generic type
floydis a member of need to have<V,E>parameterized as<String,Integer>.If it’s true that
floydbelongs to a generic type, and yetfloydalso needs to be a generic method with its own type parameters, you may want to choose different names, so as not to hide one another.References
On generic type vs generic methods
The choice between which solution route to take depends on what
floyddoes and other things. One essential question is this: do you considerfloydto be a method that belongs specifically to the generic typeGraphMatrixDirected<V,E>(answer is probably no) or is it rather a generic utility method that works with anyGraph<V,E>? (answer is probably yes).For example and guidance, we can also take a look at how Java Collections Framework is structured:
interface List<E>– a generic type that defines basic functionality for the typeboolean add(E e),E get(int index), etcclass Collections– providesstaticutility generic methodsstatic void shuffle(List<?> list)static <Textends Comparable<? super T>>void sort(List<T> list)static <Textends Object & Comparable<? super T>>T max(Collection<? extends T> coll)Assuming that
floydis indeed an implementation of Floyd-Warshall all-pairs shortest path algorithm, I’d argue that it should be astaticutility method to say aGraphsclass, and works with anyGraph<V,E>.See also, Effective Java 2nd Edition
Generic types vs methods:
Interfaces: