Reputation: 835
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);
}
}
}
}
}
}
Upvotes: 1
Views: 213
Reputation: 383746
Perhaps you meant floyd
to be a generic method?
public <V,E> void floyd(GraphMatrixDirected<V,E> g)
Otherwise, whatever generic type floyd
is a member of need to have <V,E>
parameterized as <String,Integer>
.
If it's true that floyd
belongs to a generic type, and yet floyd
also 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.
The choice between which solution route to take depends on what floyd
does and other things. One essential question is this: do you consider floyd
to be a method that belongs specifically to the generic type GraphMatrixDirected<V,E>
(answer is probably no) or is it rather a generic utility method that works with any Graph<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 type
boolean add(E e)
, E get(int index)
, etcclass Collections
- provides static
utility generic methods
static void shuffle(List<?> list)
static <T
extends Comparable<? super T>>
void sort(List<T> list)
static <T
extends Object & Comparable<? super T>>
T max(Collection<? extends T> coll)
Assuming that floyd
is indeed an implementation of Floyd-Warshall all-pairs shortest path algorithm, I'd argue that it should be a static
utility method to say a Graphs
class, and works with any Graph<V,E>
.
Generic types vs methods:
Interfaces:
Upvotes: 3
Reputation: 8898
Try
public <V, E> void floyd(GraphMatrixDirected<V,E> g)
(This should probably be static too).
Otherwise, we would have to see how you create the object containing this method, and in particular what types you assign to V and E (if V was String and E was Integer, it would work, but apparently it isn't).
I understand you have something like
class GraphMatrixDirected<V,E> extends GraphMatrix<V,E> { ... }
Then you could keep the method signature as you had it (actually if it is defined in GraphMatrixDirected and it is an instance method, you probably don't want another GraphMatrixDirected parameter to it, but just use this
), and just use:
GraphMatrixDirected<String, Integer> g = new GraphMatrixDirected<String, Integer>();
Note that I don't endorse this design, but this is beyond the point.
Upvotes: 3